• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

JSON array to Java object

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently I am making a AJAX call passing a JSON array to the server. The String which comes in to my Java class looks like this...

[{itemName: "apple",itemValue: "10.00"}, {itemName: "orange",itemValue: "20.10"}, {itemName: "banana", itemValue: "33.00"}]
I need to parse this into an array or List of Item.java classes



I tried JSONArray from org.json



but this just creates an array of JSONObject.

Are there any utilities which will take the json array string i have listed along with Item.class as input and give me back an array or list of Item classes.

Thanks,
Ram
 
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had good luck with Gson.
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any API in gson which will do what i am looking for. I checked the API's and dont see any which will do what i am looking for.
 
Bear Bibeault
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gson.fromJson()
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks looks like that will work. When i tried using it am getting a java.lang.LinkageError: LinkageError when instantiating Gson.

We are using gson 1.3 jar and our project is a java 1.4 project. What version of Java does gson 1.3 jar use.. ??

Will I get a LinkageError if a java 1.4 project tries to use APIs in Java 5 ??

My dev server is WAS 6.0.

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that was the problem....i have to be in java 5 to use gson 1.3. Compiled with 5 and tried it worked like a champ. Gson rocks -

Thanks for your inputs Bear.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic