This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/CC3220SF-LAUNCHXL: JSON APP : ARRAY MEMBER : GET MEMBER COUNT

Part Number: CC3220SF-LAUNCHXL
Other Parts Discussed in Thread: CC3220S

Tool/software: Code Composer Studio

Hello,

We are using cc3220S_LAUNCHXL json app, we have created the json template with 3 members inside the array.

it is working fine , but in our application array element count is not fixed, sometimes number if members can be 4 and sometimes it can be 6.

so , in that case how we can handle it while parsing??  can we define max size of array members ?

another thing  Json_getArrayMembersCount  returns, count of members defined in template. How to get actual populated member count ?

example the function "Json_getArrayMembersCount" returns incorrect count in case there is a mismatch is number of elements.

example

{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}

  • Hi Mrinal,

    When you're thinking about parsing arrays within a JSON object, are you wondering about instances in which you would want to loop over the array's members? If so, I would account for the variable number of members by creating a template with the maximum amount of members you plan to have. You will still be able to use Json_getArrayMembersCount() to define an upper limit for any loops you want to do over the structure.

    Unfortunately, the API does not provide a function to retrieve the number of defined members in an actual json array, only the amount defined by the template, as you've seen. To account for this (in some instances) , when looping through an array, you can check the return value of Json_getValue(). In the case of a json object with type "string", when your program attempts to get the value of the unset portion of the array, the return will be an empty string, and you'll know you've reached the section of your array that hasn't been defined.

    I understand this isn't a cure-all for any situation you may run into with the library and a particular json template, but it should suffice for the example you gave. Please feel free to ask a follow-up if you have a specific example in which you're unsure how to deal with the mismatch between the json template and the actual defined json.

    Best,
    Brandon
  • Hello Brandon,

    I had similar issue, thanks for the clarification.

    One more doubt if we want to define maximum no. of array members, like 20 elements, is it possible to do it by using number 20,
    or we will have to write array member 20 times in template.
  • Hi Sagar,

    You will have to explicitly list the elements of any array that you want the library to make space for. It would be convenient, but because the elements of these arrays don't all have to be of the same type, that may be one possible reason why specifying a number for the size isn't a default feature of the library.

    Brandon