Thanks got Encoding working for the required JSON. Any pointers on how to get the decoding working ?
From the example https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c <https://github.com/apache/mynewt-core/blob/master/encoding/json/test/src/testcases/json_simple_decode.c> Am able to decode upto name2 only. {“name1": 1,”name2": “value2”, “name3”: [{“name4”: 2, “name5”: 3}]}; Please let me know the Structure for Array of objects. struct json_attr_t test_attr[3] = { [0] = { .attribute = “name1", .type = t_integer, .addr.integer = &int_val, .nodefault = true }, [1] = { .attribute = “name2", .type = t_string, .addr.string = string1, .nodefault = true, .len = sizeof(string1) }, [2] = { .attribute = “name3", .type = t_array, .addr.array = { .element_type = t_integer, .arr.integers.store = intarr, .maxlen = sizeof intarr / sizeof intarr[0], .count = &array_count, }, .nodefault = true, .len = sizeof(intarr) }, [6] = { .attribute = NULL } }; > On 28-Feb-2018, at 2:05 PM, marko kiiskila <ma...@runtime.io> wrote: > > > >> On Feb 28, 2018, at 10:10 AM, Aditya Xavier <adityaxav...@me.com> wrote: >> >> Yes, that was derp from my end. >> >> Any clue on how to encode the JSON structure I mentioned ? >> >> Is this method correct ? > > You need to initialize the encoder; it needs to be told where to write > the encoded data. Specifically, you need to at least fill in je_write > function pointer. This is who’ll get the stream of encoded data. > > Take a look at unit test @ > encoding/json/test/src/testcases/json_simple_encode.c > > Otherwise it looks pretty good, based on my cursory inspection. > >> >> struct json_encoder *encoder, *module; >> struct json_value data; >> >> memset(&encoder, 0, sizeof(encoder)); >> memset(&module, 0, sizeof(module)); >> >> json_encode_object_start(encoder); >> >> JSON_VALUE_INT(&data, 1); >> json_encode_object_entry(encoder, “name1", &data); >> >> JSON_VALUE_STRING(&data, “value2"); >> json_encode_object_entry(encoder, “name2", &data); >> >> json_encode_array_name(encoder, “name3"); >> json_encode_array_start(encoder); >> >> if(getType){ >> json_encode_object_start(module); >> >> JSON_VALUE_INT(&data, 4); >> json_encode_object_entry(module, “name4", &data); >> >> JSON_VALUE_INT(&data, 5); >> json_encode_object_entry(module, “name5", &data); >> >> json_encode_object_finish(module); >> } >> >> json_encode_array_finish(encoder); >> json_encode_object_finish(encoder); >> >> >> >>> On 28-Feb-2018, at 1:18 PM, marko kiiskila <ma...@runtime.io> wrote: >>> >>> Hi Aditya, >>> >>>> On Feb 28, 2018, at 9:26 AM, Aditya Xavier <adityaxav...@me.com> wrote: >>>> >>>> HI Mynewt Team, >>>> >>>> Wanted some assistance on how to encode and decode the following JSON >>>> string. >>>> >>>> {“name1":1,”name2”:"value2”,"name3":[{“name4":1,”name5":5}]} >>>> >>>> Because of sparse documentation, I used the Test sample; however am >>>> stuck with encoding an array of objects. >>>> >>> >>> … >>> >>>> >>>> I followed the example provided at >>>> https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry >>>> <https://mynewt.apache.org/latest/os/modules/json/json_encode_object_entry> >>>> >>>> And it gives me error :- >>>> error: implicit declaration of function 'json_encode_object_start' >>>> >>>> Thanks, >>>> Aditya Xavier. >>> >>> you need to #include <json/json.h> to see function/macro declarations. >>> >> >