change a json field

428 views
Skip to first unread message

Vas K

unread,
Apr 25, 2013, 8:25:46 AM4/25/13
to jansso...@googlegroups.com
After a curl request on my server i get this response:
{ "_id":"b63",
"_rev":"1-15d",
"cmdText":"130104",
"ndNumber":9999,
"cmdCode":4,
"tmStamp":"2013-01-04 11:56:53",
"AnsText":";9999;04;No Data;" }
How can I change the field ndNumber and the field AnsText to something else(number and string) and then output the json into a string to send it back to the server?
I am already using json_loads to access the json and json_object_get to access the "ndNumber" and "AnsText" fields. 

Petri Lehtinen

unread,
Apr 26, 2013, 12:11:51 AM4/26/13
to jansso...@googlegroups.com
You're almost there. Now you just create new values using
json_integer() and json_string(), and use json_object_set_new() to replace
the values in the original object:

json_object_set_new(obj, "ndNumber", json_integer(1234));
json_object_set_new(obj, "AnsText", json_string("foo bar"));

Then serialize the object using json_dumps() and send it back.

Petri

Vas K

unread,
Apr 26, 2013, 7:27:18 AM4/26/13
to jansso...@googlegroups.com
thank you.I use json_object_set either for a new object or change the value of an old one.I hope its ok.

Petri Lehtinen

unread,
Apr 26, 2013, 7:29:27 AM4/26/13
to jansso...@googlegroups.com
Vas K wrote:
> thank you.I use json_object_set either for a new object or change the value of
> an old one.I hope its ok.

Yes, it's OK.

rogerz

unread,
Apr 26, 2013, 5:45:03 PM4/26/13
to jansso...@googlegroups.com
On Friday, April 26, 2013, Vas K wrote:
thank you.I use json_object_set either for a new object or change the value of an old one.I hope its ok.

I think you should be careful about memory leak when using json_object_set().

    json_object_set(obj, "ndNumber", json_integer(1234)); 

The extra ref to anonymous json_integer(1234) will keep it in memory when you do json_decref(obj).
    

--
--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users
---
You received this message because you are subscribed to the Google Groups "Jansson users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jansson-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
rogerz

vkefa...@gmail.com

unread,
Apr 26, 2013, 5:51:21 PM4/26/13
to jansso...@googlegroups.com
thanks for the advice. but i dont understand. how can i avoid the memory leak?

Sent from my android device.
You received this message because you are subscribed to a topic in the Google Groups "Jansson users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jansson-users/-rX_BQEX3Lg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to jansson-user...@googlegroups.com.

rogerz

unread,
Apr 26, 2013, 6:44:10 PM4/26/13
to jansso...@googlegroups.com
On Saturday, April 27, 2013, wrote:
thanks for the advice. but i dont understand. how can i avoid the memory leak?

use API with suffix "_new" for anonymous json_t so the reference won't be increased but borrowed from source, as in the example given by Petri.

json_object_set() is suitable for referencing existing json_t. Suppose you are extracting ns from response and use it in another obj.

// response = {nd: 1234, ns: "str"}

json_t *obj = json_object();
json_object_set(obj, "res_nd", json_object_get(response, "nd"));

json_decref(response);
json_decref(obj);

There's no auto garbage collection in C, so we should be really careful about the reference management in jansson.

I wonder if Petri has any good suggestion on it. I sometimes make mistake on it by myself.




Sent from my android device.

-----Original Message-----
From: rogerz <rogerz...@gmail.com>
To: "jansso...@googlegroups.com" <jansso...@googlegroups.com>
Sent: Sat, 27 Apr 2013 0:45
Subject: Re: [jansson-users] change a json field
 
On Friday, April 26, 2013, Vas K wrote:
thank you.I use json_object_set either for a new object or change the value of an old one.I hope its ok.

I think you should be careful about memory leak when using json_object_set().

    json_object_set(obj, "ndNumber", json_integer(1234)); 

The extra ref to anonymous json_integer(1234) will keep it in memory when you do json_decref(obj).


--
rogerz
Reply all
Reply to author
Forward
0 new messages