]> saetta.ns0.it Git - libzakutilsjsonxml/commitdiff
Reading of array in Json::to_xml.
authorAndrea Zagli <azagli@libero.it>
Thu, 23 Jul 2020 07:55:01 +0000 (09:55 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 23 Jul 2020 07:55:01 +0000 (09:55 +0200)
examples/sample.json
src/json.c

index fd4348f4ae6aa6bfd473219609a01dcc619b775a..50a75e7c29fea02a82fea90e95b33c32b55aabab 100644 (file)
@@ -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"}]}
+       ]
 }
index 8d0b4fd4e8fcc342bfb188086760a0b7bc6f1417..aab1b3c7126743da97e09b51defcaa1676fab841 100644 (file)
@@ -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);
                }