4

From jsp through ajax cal I'm passing json string to server and I am converting to json object. How can I convert the jsonobject to a model class object in java?

In server I'm doing this:

 HttpServletRequest request = ServletActionContext.getRequest();
 String jsonData = request.getParameter("JsonData");
 JSONObject jsonDataObject = (JSONObject) JSONSerializer.toJSON( jsonData );

My model classes looks like this:

   public class Vehicles {

private List<Vehicle> vehicle;

public List<Vehicle> getVehicle() {
    return vehicle;
}

public void setVehicle(List<Vehicle> vehicle) {
    this.vehicle= vehicle;
}

    }

And

  public class Vehicle{
    private Integer vId;
   private String VName;
    private List<Department> department;
   //getters and setters;
    }

and

  public class Department{
    private Integer depId;
private String departmentName;
private List<Item> item;
   //getters and setters
   }

and

  public class Item{
  private Integer itemId;
  private String itemName;
  //getters and setters
   }

and I am getting jsonData String as

{"vehicles":[{"vehicle":[{"department":[{"Item":[{"itemId":31,"itemName":"c7"},{"itemId":32,"itemName":"c2"}],"depId":21,"departmentName":"d1"}],"vId":11,"VName":"aaa"},{"department":[{"Item":[{"itemId":33,"itemName":"c3"},{"itemId":34,"itemName":"c4"}],"depId":22,"departmentName":"d2"},{"Item":[{"itemId":36,"itemName":"c1"}],"depId":24,"departmentName":"d3"}],"vId":12,"VName":"bbbb"},{"department":[{"Item":[{"itemId":30,"itemName":"c6"},{"itemId":35,"itemName":"c5"}],"depId":23,"departmentName":"d4"}],"vId":13,"VName":"cccc"},{"department":[{"Item":[{"itemid":37,"itemName":"c8","status":0}],"depId":25,"departmentName":"d5"}],"vId":14,"VName":"ddd"}]}]}

How can I convert JSONObject jsonDataObject ( or String jsonData) to model class object(ie vehicles) in java?

4
  • Make sure you've configured the serializer using JsonConfig and then you may use JSONSerializer.toJava. Commented Aug 10, 2012 at 7:16
  • I'd like to suggest - Gson API. Commented Aug 10, 2012 at 7:29
  • 1
    This looks like a duplicate: stackoverflow.com/questions/1395551/… Commented Apr 30, 2013 at 14:44
  • @AndersonGreen I don't think this is an exact duplicate. That question asked how to turn a JSON string into a simple Object; this question asks how to turn a JSON string into a particular type of classed object. That other question is perfectly happy to end up with a JSONObject; this question wants a Vehicle. Commented Apr 30, 2013 at 15:50

2 Answers 2

5

use this..

  import org.codehaus.jackson.map.ObjectMapper;
  import org.json.JSONException;
  import org.json.JSONObject; 


HttpServletRequest request = ServletActionContext.getRequest();
Vehicles vehicles;
String jsonData = request.getParameter("JsonData");
jsonData = jsonData.substring(13, jsonData.length()-2);
ObjectMapper mapper = new ObjectMapper();
try{
    vehicles= mapper.readValue(jsonData, Vehicles.class);
}
catch (Exception e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if he has a collection of vehicles?
I dont know what is the purpose of subString json String. We can directly map JSON String to the respective pojo class with ObjectMapper.
1

For converting (String jsonData), i prefer Gson

Its like,

 Gson gsonObj = new Gson();
 Vehicles vehicles = gsonObj.fromJson(jsonData, Vehicles.class);
 iterate through this list

7 Comments

i assume you get jsonData as json string and its value in valid format, then Gson should parse it for you, just share the json string.
1. Item has to be item, since in Item pojo you have item as instance variable, 2. {"vehicles":[ is not required, since your Vehicles pojo has only vehicle as instance variable. Change those you will have it working.
but the jsonData is created dynamically How can I remove " {"vehicles":[ " part
Then create a class BaseVehicles and have List<Vehicles> vehicles as instance variable and try to parse
BaseVehicles baseVehicles = gsonObj.fromJson(jsonData, BaseVehicles .class); inside BaseVehicles created List<Vehicles> with getters ans setters, Still baseVehicles is showing null value
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.