From 01f2ec834a6e0b1a12bc51d1406bbdbe7849e0f7 Mon Sep 17 00:00:00 2001 From: Arun Muthusamy <arun.muthusamy@inteno.se> Date: Wed, 13 Jun 2018 10:42:55 +0200 Subject: [PATCH] update function get_json_string_object_by_key --- common.c | 47 ++++++++++++++++++----------------------------- common.h | 2 +- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/common.c b/common.c index 257496b..2bebc63 100644 --- a/common.c +++ b/common.c @@ -52,38 +52,27 @@ struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb) return bb; } -//free memory "return_object" -struct json_object *json_parse_string(json_object *jsonContainer, char *string) +struct json_object *get_json_string_object_by_key(json_object *json_obj, char *string) { - json_object_iter iterator; - json_object *return_object; - enum json_type type; - int string_found = 0; - - type = json_object_get_type(jsonContainer); - if (type == json_type_object) { - json_object_object_foreachC(jsonContainer, iterator) { - //prints the key.. - printf("iteratorKey is :%s\n", iterator.key); - if (strcmp(string, (char *)iterator.key) == 0) { - return_object = json_object_object_get(jsonContainer,iterator.key); - printf("Input string value: %s\n", (char *)json_object_get_string(iterator.val)); - string_found = 1; - break; - } - } - } - if (string_found) - return return_object; - else - goto string_not_found; -fail_calloc: -string_not_found: - return NULL; + json_object_iter iterator; + json_object *string_object = NULL; + enum json_type type; + + type = json_object_get_type(json_obj); + if (type == json_type_object) { + //search the key from the json object + json_object_object_foreachC(json_obj, iterator) { + printf("iteratorKey is :%s\n", iterator.key); + if (strcmp(string, (char *)iterator.key) == 0) { + string_object = json_object_object_get(json_obj,iterator.key); + printf("Input string value: %s\n", (char *)json_object_get_string(iterator.val)); + break; + } + } + } + return string_object; } - - void xml_to_json(xmlNode *anode, json_object *jobj) { xmlNodePtr cur_node = NULL; diff --git a/common.h b/common.h index 77f6053..5410559 100644 --- a/common.h +++ b/common.h @@ -82,5 +82,5 @@ int validate_puk_format(char *puk); int validate_pin_format(char *pin); int pin_status(struct blob_buf *bb, struct device *dev); int check_response(struct json_object *response); -struct json_object *json_parse_string(json_object *jsonContainer, char *string); +struct json_object *get_json_string_object_by_key(json_object *json_obj, char *string); #endif -- GitLab