Re: [json-lib-user] Remove name value pair from JSONObject
Brought to you by:
aalmiray
|
From: Andres A. <aal...@ya...> - 2007-12-07 00:32:08
|
If I understand your json correctly you have the following
{
"sizewidth" : 940,
"style" : "width:940px;",
"name" : "",
"css" : ""
}
so you'll have to call
JSONObject jsonObject = JSONObject.fromObject( imgBean );
jsonObject.remove( "style" );
because
remove() returns the removed value, not the instance of a JSONObject
where it was invoked, meaning you can't chain remove the same way as
you can chain element or accumulate, reason being is that remove comes
from the Map interface. Perhaps the addition of a chained remove-like
method is in order.
Another way to solve your problem is to use property exclusions
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExclusions( new String[ ] {
"style" } );
JSONObject jsonObject = JSONObject.fromObject( imgBean, jsonConfig );
assertFalse( jsonObject.has("style") )
Cheers,
Andres
----- Original Message ----
From: Haritha Juturu <har...@ya...>
To: jso...@li...
Sent: Thursday, December 6, 2007 4:22:22 PM
Subject: [json-lib-user] Remove name value pair from JSONObject
Hi Everyone,
I am using JSON-LIB to serialize json to java objects.
I have a java object ImageBean.When i do a JSONObject.fromObject( imgBean ) the output is {"sizeWidth":940,"style":"width:940px;","name":"","css":""}
I am trying to remove width:940px name-value pair from this JSONObject by using this code snippet
JSONObject.fromObject( imgBean ).remove("width");
When i print the JSONObject invoking remove() i get "940px" as the output.
Is there a way to remove a name-value pair from the JSONObject and get the final output as {"sizeWidth":940,"name":"","css":""}
Cheers
Haritha
Looking for last minute shopping deals?
Find them fast with Yahoo! Search.
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping |