From: Andrea Zagli Date: Thu, 23 Jul 2020 07:55:01 +0000 (+0200) Subject: Reading of array in Json::to_xml. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=522a595b69fdc8d3f73e4c63b65a4521b33aa23f;p=libzakutilsjsonxml Reading of array in Json::to_xml. --- diff --git a/examples/sample.json b/examples/sample.json index fd4348f..50a75e7 100644 --- a/examples/sample.json +++ b/examples/sample.json @@ -1,5 +1,8 @@ { - "obj1": "value1", - "obj2": "value2", - "obj3": "value3" + "memb1": "value1", + "memb2": "value2", + "memb3": [ + {"name": "first", "color": "red"}, + {"name": "second", "color": "green", "arr1": [{"arr1_1": "val1_1"},{"arr1_2": "val1_2", "arr1_2_1": "val1_2_1"}]} + ] } diff --git a/src/json.c b/src/json.c index 8d0b4fd..aab1b3c 100644 --- a/src/json.c +++ b/src/json.c @@ -111,7 +111,28 @@ zak_utils_json_to_xml (JsonReader *reader, xmlNode *xnode) members = json_reader_list_members (reader); for (i = 0; i < l; i++) { - xmlNewTextChild (xnode, NULL, (const xmlChar *)members[i], (const xmlChar *)zak_utils_json_get_string (reader, members[i])); + json_reader_read_member (reader, members[i]); + if (json_reader_is_array (reader)) + { + xmlNode *cur; + guint a; + guint j; + + j = json_reader_count_elements (reader); + for (a = 0; a < j; a++) + { + json_reader_read_element (reader, a); + cur = xmlNewNode (NULL, (const xmlChar *)members[i]); + zak_utils_json_to_xml (reader, cur); + xmlAddChild (xnode, cur); + json_reader_end_element (reader); + } + } + else + { + xmlNewTextChild (xnode, NULL, (const xmlChar *)members[i], (const xmlChar *)json_reader_get_string_value (reader)); + } + json_reader_end_member (reader); } g_strfreev (members); }