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?

subbusaabu
  • 181
  • 3
  • 3
  • 12
  • Make sure you've configured the serializer using [`JsonConfig`](http://json-lib.sourceforge.net/apidocs/net/sf/json/JsonConfig.html) and then you may use [`JSONSerializer.toJava`](http://goo.gl/he1Fl). – obataku Aug 10 '12 at 07:16
  • I'd like to suggest - `Gson` API. – KV Prajapati Aug 10 '12 at 07:29
  • 1
    This looks like a duplicate: http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java?rq=1 – Anderson Green Apr 30 '13 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`. – apsillers Apr 30 '13 at 15:50

2 Answers2

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();
}
Brittas
  • 491
  • 1
  • 5
  • 18
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
  • 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. – Gopi Shankar Aug 10 '12 at 09:49
  • 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. – Gopi Shankar Aug 10 '12 at 11:38
  • but the jsonData is created dynamically How can I remove " {"vehicles":[ " part – subbusaabu Aug 10 '12 at 12:55
  • Then create a class BaseVehicles and have List vehicles as instance variable and try to parse – Gopi Shankar Aug 10 '12 at 13:06
  • BaseVehicles baseVehicles = gsonObj.fromJson(jsonData, BaseVehicles .class); inside BaseVehicles created List with getters ans setters, Still baseVehicles is showing null value – subbusaabu Aug 13 '12 at 05:15
  • Make sure the keys in the json string and Pojo's instance variables are same case, it should work. – Gopi Shankar Aug 13 '12 at 05:51
  • :JSONObject jsonDataObject = (JSONObject) JSONSerializer.toJSON( jsonData );String value = jsonDataObject.getString("vehicles");Vehicles vehicles= gsonObj.fromJson(value, Vehicles.class); i tried like this also its giving an exception Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 – subbusaabu Aug 13 '12 at 10:49