Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JSON array to Java object

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've had good luck with Gson.
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gson.fromJson()
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic