How does one unpack an object with a variable length array

665 views
Skip to first unread message

gerb...@gmail.com

unread,
Dec 27, 2012, 1:49:29 AM12/27/12
to jansso...@googlegroups.com
Hi,

I'm having some trouble figuring out how to unpack an object with a variable length array. I've tried several things to no avail. I've included example json below. A pointer in the right direction would be much appreciated.

 {
  "version": -1,
  "changed": 1,
  "name": "cdef1",
  "super_id": "",
  "desc": "class def 1",
  "id": "",
  "memberdef": [
    {
      "value": "member1val",
      "mask": "9999",
      "desc": "member def 1",
      "type": 6,
      "version": -1,
      "id": "",
      "name": "mdef1",
      "changed": 1
    },
    {
      "value": "member2val",
      "mask": "",
      "desc": "member def 2",
      "type": 6,
      "version": -1,
      "id": "",
      "name": "mdef2",
      "changed": 1
    }
  ]
}


Thanks,
-G

Petri Lehtinen

unread,
Dec 27, 2012, 3:23:57 AM12/27/12
to jansso...@googlegroups.com
gerb...@gmail.com wrote:
> Hi,
>
> I'm having some trouble figuring out how to unpack an object with a variable
> length array. I've tried several things to no avail. I've included example json
> below. A pointer in the right direction would be much appreciated.

With json_unpack(), you can use the 'o' format specifier to extract
the array value as a json_t pointer. You can then iterate the array
using a normal for loop, and apply json_unpack() to each element of
the array at a time.

Petri

gerb...@gmail.com

unread,
Dec 27, 2012, 4:38:58 AM12/27/12
to jansso...@googlegroups.com
Hi,

Perfect! That's just what I needed.

BTW: This is a really nice library. Excellent work!

Thanks,
-G
Reply all
Reply to author
Forward
0 new messages