Search code examples
cjsonparsingserializationmicrocontroller

How to construct JSON array with Parson?


I'm using Parson library to send sensor data from a MCU to a server. I want to generate the following JSON, but I can't figure out how to generate the arrays ("sensors" and "measurements").

{
  "systemInfo:": {
  "hubId": "1234",
  "battery:": {
      "value": 3.3,
      "unit": "V"
   }
 },
 "sensors": [
 {
   "name": "S1",
   "measurements:": [
    {
      "measuredValue": "val",
      "value": 123,
      "unit": "unit"
    }
  ]
 },
 {
   "name": "S2",
   "measurements": [
    {
      "measuredValue": "val1",
      "value": 123,
      "unit": "unit1"
    },
    {
      "measuredValue": "val2",
      "value": 123,
      "unit": "unit2"
    }
  ]
 },
 {
   "name": "s3",
   "measurements": [
    {
      "measuredValue": "val",
      "value": 120,
      "unit": "unit"
    }
   ]
  }
 ]
}

There is an example on the GitHub page (serialization_example), that generates an array by parsing a string:

json_object_dotset_value(root_object, "contact.emails",
                         json_parse_string("[\"email@example.com\", \"email2@example.com\"]"));

but I would like to generate it using the API functions and not by manually constructing the string like in the example above. E.g., by using

json_object_set_string()
json_object_dotset_string()
json_object_dotset_number() etc.

Is it possible? Or the API does not offer this functionality?


Solution

  • I didn't find a solution to my problem, but instead in found another library, cJSON, that can do what I need.