diff options
-rw-r--r-- | libs/json.c | 36 | ||||
-rw-r--r-- | 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 <string.h> #include <json-c/json.h> #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-c/json.h> json_object* jsonParseString(char* stringedJSON); +json_object* navigateToVideos(json_object* jsonRoot); |