我想使用GSON的fromJson()将我的json对象转换为我的java对象

时间:2014-07-02 08:42:15

标签: java json gson

我有一个像这样的json对象:

[
    {
        "created_at": "2014-07-01 5:01:10",
        "status": "in progress",
        "device_id": "1234",
        "order_details": [
            {
                "item_id": 1,
                "quantity": 2
            },
            {
                "item_id": 2,
                "quantity": 3
            }
        ]
    }
]

在java中我有两个类的顺序和订单细节,如下所示:

1)Order.java

package dto;
import java.util.ArrayList;
public class Order 
{
int order_id;

String created_at;
String status;
String device_id;
ArrayList<Order_details> orderList= new ArrayList<Order_details>();

 public int getOrder_id() {
    return order_id;
}
public void setOrder_id(int order_id) {
    this.order_id = order_id;
}

 public String getCreated_at() {
    return created_at;
}
 public void setCreated_at(String created_at) {
    this.created_at = created_at;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getDevice_id() {
    return device_id;
}

public void setDevice_id(String device_id) {
    this.device_id = device_id;
}

public ArrayList<Order_details> getOrderList() {
    return orderList;
}

public void setOrderList(ArrayList<Order_details> orderList) {
    this.orderList = orderList;
}

 public void attachOrderDetails(ArrayList<Order_details> member) { 
        this.orderList = member; 

}
}

2)Order_details.java

  package dto;
  public class Order_details 
  {
   int item_id;
   int quantity;
   String item_name;
   double price;

    public int getItem_id() 
    {
 return item_id;
    }
    public void setItem_id(int item_id) 
    {
this.item_id = item_id;
    }
    public int getQuantity() 
    {
 return quantity;
    }
    public void setQuantity(int quantity) 
    {
this.quantity = quantity;
    }
   public String getItem_name() 
   {
return item_name;
    }
    public void setItem_name(String item_name) 
    {
this.item_name = item_name;
    }
    public double getPrice() {
return price;
   }
   public void setPrice(double price) {
this.price = price;
    }

   }

现在我想将上面的json对象转换为order类的对象 为了达到这个目的,我试过了:

Order order= new Order();
order= gs.fromJson(json,Order.class);
System.out.println("order"+order);

但它会抛出异常,如下所示:

   com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected           BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at       com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapte   rFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:791)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)

请帮我把我的json转换成我的java对象。

3 个答案:

答案 0 :(得分:0)

当您使用GSON时,您应该更加了解区分大小写的w.r.t密钥名称和变量名称。

either change variable name to 
ArrayList<Order_details> order_details= new ArrayList<Order_details>(); instead of 
ArrayList<Order_details> orderList= new ArrayList<Order_details>();

or 

change json key name to orderList instead of order_details

答案 1 :(得分:0)

json中的第一个[表示它持有数组或Order对象。因此,您需要列出订单而不是单个订单类。

List<Order> order = new ArrayList<Order>();
order = gs.fromJson(json, order.getClass());
System.out.println("order" + order);

或使用类型标记作为

List<Order> order = new ArrayList<Order>();
Type listType = new TypeToken<List<Order>>() {}.getType();
order = gs.fromJson(json, listType);
System.out.println("order" + order);

或者直接使用您的代码,您需要将json修改为

{
  "created_at": "2014-07-01 5:01:10",
  "status": "in progress",
  "device_id": "1234",
  "order_details": [
    {
      "item_id": 1,
      "quantity": 2
    },
    {
      "item_id": 2,
      "quantity": 3
    }
  ]
}

答案 2 :(得分:0)

您的JSON字符串无法直接转换为您的类,因为您的json包含类Order的参数数组,并将其转换为类对象,您需要Order类的列表,以便此数组可以转换为Order列表。
为此,您需要获取Order类的List<Order>,如下所示:

List<Order> orderList = new ArrayList<Order>();
Type listType = new TypeToken<List<Order>>(){}.getType();
orderList = json.fromJson(jsonStr, listType);  

愿这对你有所帮助。