0

Kindly need your help as this really taking me long time to try. From JSP, I passed the stingnify JSON object as a String to the Java action, it like

String jsonDealer = [{"dealerID":"VARSSWK103","dealerName":"Dealer ABC"}, {"dealerID":"VARSSTH008","dealerName":"Dealer XYZ"}]

How I can convert this to JSON object/ or ArrayList of Dealer, so that I can retrieve the dealer ID and dealer name?

Thanks for all help...

asked Dec 14, 2010 at 9:32

4 Answers 4

4

You'll need a JSON deserializer. There are quite a few for Java listed at the bottom of the JSON.org page. As of this writing:

...and 10 more. :-)

answered Dec 14, 2010 at 9:34
Sign up to request clarification or add additional context in comments.

Comments

2

First, download Google GSON.

Second, create this class:

class Dealer {
 Dealer() {}
 public void setDealerID(String dealerID) {
 this.dealerID = dealerID;
 }
 public String getDealerID() {
 return dealerID;
 }
 public void setDealerName(String dealerName) {
 this.dealerName = dealerName;
 }
 public String getDealerName() {
 return dealerName;
 }
 private String dealerID;
 private String dealerName;
}

Third, use this code:

String jsonDealer = "[{\"dealerID\":\"VARSSWK103\",\"dealerName\":\"Dealer ABC\"}, {\"dealerID\":\"VARSSTH008\",\"dealerName\":\"Dealer XYZ\"}]";
Gson gson = new Gson();
Type type = new TypeToken<List<Dealer>>(){}.getType();
List<Dealer> fromJson = gson.fromJson(jsonDealer, type);
System.out.println(fromJson.get(0).getDealerName()); // example usage
answered Dec 14, 2010 at 9:49

3 Comments

+1, although there are, of course, lots of other options. Google GJSON isn't the only game in town...
@T.J. Crowder: of course, but since I have only GSON set up, why not give a concrete example?
Sure, and that's why I voted it up. It's just that the answer doesn't give any indication that one has options. I'm a big fan of options.
1

You will need a json parsing api like gson to parse the json string. Follow the simple tutorial in my website http://preciselyconcise.com/apis_and_installations/json_to_java.php

answered Jan 27, 2014 at 18:30

Comments

0

You probably need a JSON library for Java which can parse the string into an object / collection.

I'm not a Java expert myselft, but this list might have somethng suitable: http://www.json.org/java/

answered Dec 14, 2010 at 9:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.