How to append a json_array() to an existing json_array

902 views
Skip to first unread message

AllanL

unread,
Jun 10, 2012, 5:15:24 PM6/10/12
to Jansson users
Hello,

I am new to the jansson group....

I am building an array dynamically. I have a number of "Groups" that
are arrays of kv pairs. I would like to send them together with the
group name.

I can build the array no problem. I build it in a while loop
iterating through the group, using json_append_array(). I would like
to append the completed array to another array like :

json_array_append( array, json_pack("{"s:s","s:o"}, "Group",
"top", "values", array}})

but that doesn't work. Is there a way to do this?

I would like the final format to sort of like this :
[{ "Group" : "top" , "values" : {{ "v1":1}, {"v2":2}, {"v3":
3} .......}}]

I hope I have explained well enough

thx for any help.

rogerz

unread,
Jun 10, 2012, 6:51:33 PM6/10/12
to jansso...@googlegroups.com
Something wrong with your format string.


On Monday, June 11, 2012, AllanL wrote:
    json_array_append( array, json_pack("{"s:s","s:o"}, "Group",
"top", "values", array}})

but that doesn't work.  Is there a way to do this?

Try this.
json_array_append_new( array_group, json_pack("{ssso}", "Group", "top", "values", array));
You should use the API with new because the json pack result is referenced by the array group only.

I would like the final format to sort of like this :
  [{ "Group" : "top" , "values" : {{ "v1":1}, {"v2":2}, {"v3":

It seems not a valid json. Should it be [{"Group": "top", "values": [{"v1", 1}, {"v2", 2}, ...]}, {...}, ...] or [{"Group": "top", "values": {"v1": 1, "v2": 2, ...}}, ...]


--
 ,---.  Rogerz Zhang
( @ @ ) Human, not octopus
 ).-.(  Chase what you love. Let the rest go.
'/|||\` Code > Github | Note > Tumblr | Random > twitter 微博 | Share > Google+ 
  '|`   AsciiArt < Shimrod(hh)

AllanL

unread,
Jun 10, 2012, 7:44:10 PM6/10/12
to Jansson users
Thanks rogerz!!

you were right about my syntax--it was wrong.

That works better--I get the array added correctly, only the order is
backwards, but good enough maybe...

[{"values":[{"key1":value1},{"key2":value2},
{"key3":value3}, ...}],"Group":"top"}]

Is there a way to get {} instead of the inner []? It makes parsing
from the network easier.


thx

AL

rogerz

unread,
Jun 11, 2012, 1:13:07 AM6/11/12
to jansso...@googlegroups.com
On Mon, Jun 11, 2012 at 7:44 AM, AllanL <alla...@gmail.com> wrote:
Thanks rogerz!!

you were right about my syntax--it was wrong.

That works better--I get the array added correctly, only the order is
backwards, but good enough maybe...

The object {} uses hash key internally, which don't preserve the order. It is random
 
[{"values":[{"key1":value1},{"key2":value2},
{"key3":value3}, ...}],"Group":"top"}]
 
Is there a way to get {} instead of the inner []? It makes parsing
from the network easier.
 
If there's no need to keep the key in order, you may use object instead of array. [ { "values": {"key1": value1, "key2": value2, ...}, "Group" : "top"}... ]. Is that what you are asking for? 

BTW, to get familiar with json, I suggest you play with jsoneditoronline.org

AllanL

unread,
Jun 11, 2012, 5:34:09 AM6/11/12
to Jansson users
Thanks, I'll try that, and thank you for the jsoneditoronline.org
info!!



On Jun 11, 1:13 am, rogerz <rogerz.zh...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages