Re: [json-lib-user] Converting JSONSerializer.toJSON
Brought to you by:
aalmiray
|
From: Haritha J. <har...@ya...> - 2008-06-25 20:34:11
|
Hey Andres
I changed to the latest Json-lib jar (i.e json-lib-2.2.2-jdk15.jar) and it worked.
I was using an older version json-lib-2.2-jdk15.jar.
Thanks for your help
--Haritha
----- Original Message ----
From: Andres Almiray <aal...@ya...>
To: Haritha Juturu <har...@ya...>; jso...@li...
Sent: Wednesday, June 25, 2008 12:46:58 PM
Subject: Re: Converting JSONSerializer.toJSON
Hi Haritha,
Since the 2.x series JSONObject switched from HashMap to a ListOrderedMap (commons-collections) , there is even a test to assure this behavior should not be broken
371 public void testFR_1858073_preserveInsertionOrder() {
372 JSONObject jsonObject = new JSONObject().element( "one", "one" ).element( "two", "two" )
373 .element( "three", "three" );
374 JSONArray actual = jsonObject.names();
375 JSONArray expected = new JSONArray().element( "one" ).element( "two" ).element( "three" );
376 Assertions.assertEquals( expected, actual );
377 }
Perhaps you stumbled into a case that was not considered by the current impl. May I ask which version of Json-lib are you testing with?
-------------------------------------------
http://jroller.com/aalmiray
http://www.linkedin.com/in/aalmiray
--
What goes up, must come down. Ask any system administrator.
There are 10 types of people in the world: Those who understand binary, and those who don't.
To understand recursion, we must first understand recursion.
----- Original Message ----
From: Haritha Juturu <har...@ya...>
To: jso...@li...; aal...@ya...
Sent: Wednesday, June 25, 2008 12:07:19 PM
Subject: Converting JSONSerializer.toJSON
Hi All/Andres
I have a LinkedHashMap allPhotos = new LinkedHashMap<String, Object>();
which has objects sorted in order of insertion.
What i am trying to do is convert this to a JSON string by using the following code
JSONObject json = (JSONObject) JSONSerializer.toJSON( allPhotos );
json.toString();
When i do this the JSON that is created looses the order of the elements in allPhotos.
Example
I put data in this order
allPhotos.put('abc','david');
allPhotos.put('cdg','jason');
allPhotos.put('efg','brian');
But my final json string looks like this
{"cdg":"jason","efg":"brian","abc":"david"}
Retaining this order is important to our application. Can anyone tell me how i can achieve that?
Thanks
Haritha
|