Re: [json-lib-user] Quick Question on JSON
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2007-12-13 05:19:00
|
Hi HAritha,=0A=0AOn closer inspection Json-lib is doing the correct thing b= y not converting the value of your property 'JSON' into a JSONObject, even = if it is a well-formed json string. In a previous version it did so, but th= at caused another bug with simpler json strings. So if you want to transfor= m that json string into a JSONObject you'll have to do it yourself. There a= re several options=0A=0A-- Option A=0AJSONObject jsonObj =3D JSONObject.fro= mObject( textField, jsonConfig );=0AjsonObj.set( "JSON", JSONObject.fromObj= ect( jsonObj.getJSONObject( "JSON" ), jsonConfig );=0A=0A-- Option B=0A=0A = JsonConfig jsonConfig =3D new JsonConfig();=0A jsonConfig.registe= rJsonValueProcessor( "JSON", new JsonValueProcessor(){=0A public Ob= ject processArrayValue( Object value, JsonConfig jsonConfig ) {=0A = // not interested in processing the value inside an array=0A = return value;=0A }=0A=0A public Object processObjectValue( = String key, Object value, JsonConfig jsonConfig ) {=0A return JS= ONObject.fromObject(value);=0A }=0A } );=0A JSONObject js= onObject =3D JSONObject.fromObject( textField, jsonConfig );=0A=0ACheers,= =0AAndres=0A=0A----- Original Message ----=0AFrom: Haritha Juturu <haritha.= ju...@ya...>=0ATo: Andres Almiray <aal...@ya...>; json-lib-user@= lists.sourceforge.net=0ASent: Tuesday, December 11, 2007 1:57:52 PM=0ASubje= ct: Re: [json-lib-user] Quick Question on JSON=0A=0A=0AYes i get this excep= tion on calling JSONObject json =3D json.getJSONObject( "JSON" );=0A=0AExce= ption in thread "main" net.sf.json.JSONException: JSONObject["JSON"] is not= a JSONObject.=0A at net.sf.json.JSONObject.getJSONObject(JSONObject.jav= a:1813)=0A at com.splashnote.web.pages.JavaToJson.getJSON(JavaToJson.jav= a:132)=0AHaritha=0A=0A----- Original Message ----=0AFrom: Andres Almiray <a= al...@ya...>=0ATo: Haritha Juturu <har...@ya...>; json-li= b-...@li...=0ASent: Tuesday, December 11, 2007 1:04:11 PM= =0ASubject: Re: [json-lib-user] Quick Question on=0A JSON=0A=0A=0AHi Harith= a,=0A=0AYou are right, if it is a nested JSONObject it should print it with= out the surrounding double quotes. As a last resort, can you call=0A=0AJSON= Object jsonObj =3D JSONObject.fromObject( textField, jsonConfig );=0AJSONOb= ject json =3D json.getJSONObject( "JSON" );=0A=0AIf it really is a JSONObje= ct then no exception should arise. If no exception is thrown then there may= be something going on with the string representation of a JSONObject when = printed.=0A=0AThanks,=0AAndres=0A=0A=0A----- Original Message ----=0AFrom: = Haritha Juturu <har...@ya...>=0ATo:=0A Andres Almiray <aalmiray= @yahoo.com>; jso...@li...=0ASent: Tuesday, December = 11, 2007 12:59:32 PM=0ASubject: Re: [json-lib-user] Quick Question on JSON= =0A=0A=0AHi Andres=0A=0AThe TextField bean looks like this=0Apublic class T= extField(){ =0A private Long parentId;=0A private Long textFieldId;=0A= private String JSON;=0A}=0A=0AAfter executing the code JSONObject jsonO= bj =3D JSONObject.fromObject( textField, jsonConfig ); , i try to print th= is object 'jsonObj" and it prints the name value pair( of JSON Property) wi= th quotes=0A=0A{"parentId":678,"textFieldId":111,"JSON":"{'sizeWidth':620,'= text':'=93[Winning the Nobel Peace Prize]=0A ','positionLeft':20,'style':'l= eft:20px;top:100px;width:620px;height:100px;','positionTop':100,'name':'','= cssClass':'','objectType':'TextObject','sizeHeight':100}"}=0A=0AIf it was c= onverting this 'JSON' property to JSONObject internally, wont it print as= =0A=0A"JSON":=0A{"parentId":678,"textFieldId":111,"JSON":{'sizeWidth':620,'= text':'=93[Winning=0Athe Nobel Peace Prize]=0A','positionLeft':20,'style':'= left:20px;top:100px;width:620px;height:100px;','positionTop':100,'name':'',= 'cssClass':'','objectType':'TextObject','sizeHeight':100}}=0Awithout the qu= otes on the value.=0A=0APlease correct me if i got it wrong=0A=0AI am using= the json-lib-2.1 for jsd 1.5 version.=0AHaritha=0A=0A=0A=0A----- Original = Message ----=0AFrom: Andres Almiray <aal...@ya...>=0ATo: Haritha Jutu= ru <har...@ya...>; jso...@li...=0ASent: = Tuesday, December 11, 2007 12:27:12 PM=0ASubject: Re: [json-lib-user] Quick= Question on JSON=0A=0A=0AHi Haritha,=0A=0AAre you sure the contents of the= named property ('JSON') are not automatically converted into a JSONObject = ? it looks to me like it is a well formed json string, if so it should be c= onverted automatically when you call fromObject() at top level.=0A=0ACan yo= u please tell us which version of Json-lib are you testing with?=0A=0ARegar= ds,=0AAndres=0A=0A----- Original Message ----=0AFrom: Haritha Juturu <harit= ha....@ya...>=0ATo: jso...@li...=0ASent: Tues= day, December 11, 2007 12:16:36 PM=0ASubject: [json-lib-user] Quick Questio= n on JSON=0A=0A=0AHi Everyone=0AI have a line of code=0AJSONObject jsonObj = =3D JSONObject.fromObject( textField, jsonConfig ); =0Awhich produces this= JSON string=0A=0A{"parentId":678,"textFieldId":111,=0A"JSON":=0A"{'sizeWid= th':620,'text':'=93[Winning the Nobel Peace Prize]','positionLeft':20,'styl= e':'left:20px;top:100px;width:620px;height:100px;','positionTop':100,'name'= :'','cssClass':'','objectType':'TextObject','sizeHeight':100}"=0A}=0A=0Anow= if i want the property "json" whose value is a string=0A =93[Winning the N= obel Peace=0APrize]','positionLeft':20,'style':'left:20px;top:100px;width:6= 20px;height:100px;','positionTop':100,'name':'','cssClass':'','objectType':= 'TextObject','sizeHeight':100}"=0A=0Ato be converted to a json object .... = is there any API methods i can use.i am unable to locate any.=0A=0Ai.e my o= utput should look like =0A{"parentId":678,"textFieldId":111,=0A"JSON": thi= s value is not a string anymore=0A{'sizeWidth':620,'text':'=93[Winning=0Ath= e Nobel Peace=0APrize]','positionLeft':20,'style':'left:20px;top:100px;widt= h:620px;height:100px;','positionTop':100,'name':'','cssClass':'','objectTyp= e':'TextObject','sizeHeight':100}=0A}=0A=0AI have a constraint that i need = to do this manipulation on the jsonObj .=0A=0AThanks=0AHaritha=0A=0A=0A=0A= =0A=0A=0A Be a better friend, newshound, and =0Aknow-it-all with Yahoo= ! Mobile. Try it now.=0A=0A=0A=0A=0A=0A Be a better friend, newshound= , and =0Aknow-it-all with Yahoo! Mobile. Try it now.=0A=0A=0A=0A=0A=0A = Looking for last minute shopping deals? =0AFind them fast with Yahoo! Se= arch.=0A=0A=0A=0A=0A=0A=0A=0A=0A Never miss a thing. Make Yahoo your= homepage.=0A=0A=0A=0A=0A=0A=0A=0A Be a better friend, newshound, and = =0Aknow-it-all with Yahoo! Mobile. Try it now.=0A=0A=0A=0A=0A=0A ____= ___________________________________________________________________________= _____=0ANever miss a thing. Make Yahoo your home page. =0Ahttp://www.yahoo= .com/r/hs |