From e2ffc88b409b54f7a23d963095ea3a2439a361c1 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Wed, 1 Sep 2021 00:27:27 +0100 Subject: JSON Navigation --- libs/json.c | 36 ++++++++++++++++++++++++++++++++---- libs/json.h | 1 + 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/libs/json.c b/libs/json.c index 4b0dac0..3631c19 100644 --- a/libs/json.c +++ b/libs/json.c @@ -3,11 +3,39 @@ #include #include #include "json.h" +#define MAXLENGTH 256 json_object* jsonParseString(char* stringedJSON) { - struct json_object *json; - json = ( stringedJSON != NULL && strlen(stringedJSON) > 0 ) ? json_tokener_parse(stringedJSON) : NULL; - free(stringedJSON); - return json; + return ( stringedJSON != NULL && strlen(stringedJSON) > 0 ) ? json_tokener_parse(stringedJSON) : NULL; +} + +json_object* navigateToVideos(json_object* jsonRoot) +{ + char path[][MAXLENGTH] = { + "contents", + "twoColumnSearchResultsRenderer", + "primaryContents", + "sectionListRenderer", + "contents", + "0", + "itemSectionRenderer", + "contents" + }; + + json_object *videos = json_object_object_get(jsonRoot, path[0]); + + for( int i = 1; i < sizeof(path)/sizeof(path[0]); i++ ) + { + if ( json_object_get_type(videos) == json_type_array ) + { + videos = json_object_array_get_idx(videos, atoi(path[i])); + } + else + { + videos = json_object_object_get(videos, path[i]); + } + } + + return videos; } diff --git a/libs/json.h b/libs/json.h index 3896805..f769d9c 100644 --- a/libs/json.h +++ b/libs/json.h @@ -2,3 +2,4 @@ #include json_object* jsonParseString(char* stringedJSON); +json_object* navigateToVideos(json_object* jsonRoot); -- cgit v1.2.3