Click here to Skip to main content
15,943,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created JSON string as like ths
JavaScript
{"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"}]}]}

I tried to convert the same with the below code
JavaScript
Gson gsonObj = new Gson();
Vehicles vehicles = gsonObj.fromJson(jsonData, Vehicles.class);

but vehicles variable is showing a null value. How can I convert a json string to a pojo class object?
my pojo class are as the following
XML
public class Vehicles {
private List<Vehicle> vehicle;
public List<Vehicle> getVehicle() {
    return vehicle;
}
public void setVehicle(List<Vehicle> vehicle) {
    this.vehicle= vehicle;
}
    }

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

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

and
C#
public class Item{
 private Integer itemId;
 private String itemName;
 //getters and setters
  }
Posted
Updated 12-Dec-18 3:38am
v3

You are passing wrong value of json to the Gson Parser.
Your Json String starting from the
Quote:
{"vehicles":[{"vehicle":[{"department":[...
so the first element of the object is vehicles and in the
Quote:
Vehicles.class
there is no variable named vehicles so it can not assinging values.

You just need to update string to start from array
Quote:
{"vehicle":[{"department":[{"Item":[{...
above string will successfully run and parse into object.
 
Share this answer
 
Hi,

please let us to have complete project from this Tutorial.

BR
Mehdi
 
Share this answer
 
Comments
Richard Deeming 12-Dec-18 9:45am    
Not even vaguely attempting to answer the question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900