2014-04-15 207 views
3

什麼我有在這裏是給我下面的JSON代碼Web服務:預計BEGIN_OBJECT但BEGIN_ARRAY位於第1行第2列(小編輯)

[ 
    { 
    "_OrderDetails": [ 
     { 
     "ProductName": "FUCHS SUPER GT SAE 10W30 6X5/FP10100010102", 
     "TotalAfterDiscount_Lc": "7500", 
     "MeasureUnitName": "كرتونة", 
     "TotalPrice_Lc": "7500", 
     "PricePerUnit_Lc": "75", 
     "Quantity": "100" 
     } 
    ], 
    "Id": "274", 
    "OrderDate": "4/10/2014 12:00:00 AM", 
    "Number": "16", 
    "CustomerName": "الأسد", 
    "Note": "" 
    } 
] 

和我進行了一個Java類(實體)與getter和setter的所有數據:

package com.example.webservicetest; 

import java.util.List; 

public class Item { 

private String OrderDate; 
private String Number; 
private String Note; 
private String CustomerName; 
private String Id; 
private List<_OrderDetails> orderDetails; 



public String getOrderDate() { 
    return OrderDate; 
} 
public void setOrderDate(String orderDate) { 
    OrderDate = orderDate; 
} 
public String getNumber() { 
    return Number; 
} 
public void setNumber(String number) { 
    Number = number; 
} 
public String getNote() { 
    return Note; 
} 
public void setNote(String note) { 
    Note = note; 
} 

public String getId() { 
    return Id; 
} 
public void setId(String id) { 
    Id = id; 
} 
public String getCustomerName() { 
    return CustomerName; 
} 
public void setCustomerName(String customerName) { 
    CustomerName = customerName; 
} 
public List<_OrderDetails> getOrderDetails() { 
    return orderDetails; 
} 
public void setOrderDetails(List<_OrderDetails> orderDetails) { 
    this.orderDetails = orderDetails; 
} 
public class _OrderDetails{ 
    private String OrderId; 
    private String OrderDate; 
    private String Number; 
    private String Note; 
    private String ProductName; 
    private String TotalAfterDiscount_Lc; 
    private String MeasureUnitName; 
    private String TotalPrice_Lc; 
    private String PricePerUnit_Lc; 
    private String Quantity; 
    public String getOrderId() { 
     return OrderId; 
    } 
    public void setOrderId(String orderId) { 
     OrderId = orderId; 
    } 
    public String getOrderDate() { 
     return OrderDate; 
    } 
    public void setOrderDate(String orderDate) { 
     OrderDate = orderDate; 
    } 
    public String getNumber() { 
     return Number; 
    } 
    public void setNumber(String number) { 
     Number = number; 
    } 
    public String getNote() { 
     return Note; 
    } 
    public void setNote(String note) { 
     Note = note; 
    } 
    public String getProductName() { 
     return ProductName; 
    } 
    public void setProductName(String productName) { 
     ProductName = productName; 
    } 
    public String getTotalAfterDiscount_Lc() { 
     return TotalAfterDiscount_Lc; 
    } 
    public void setTotalAfterDiscount_Lc(String totalAfterDiscount_Lc) { 
     TotalAfterDiscount_Lc = totalAfterDiscount_Lc; 
    } 
    public String getMeasureUnitName() { 
     return MeasureUnitName; 
    } 
    public void setMeasureUnitName(String measureUnitName) { 
     MeasureUnitName = measureUnitName; 
    } 
    public String getTotalPrice_Lc() { 
     return TotalPrice_Lc; 
    } 
    public void setTotalPrice_Lc(String totalPrice_Lc) { 
     TotalPrice_Lc = totalPrice_Lc; 
    } 
    public String getPricePerUnit_Lc() { 
     return PricePerUnit_Lc; 
    } 
    public void setPricePerUnit_Lc(String pricePerUnit_Lc) { 
     PricePerUnit_Lc = pricePerUnit_Lc; 
    } 
    public String getQuantity() { 
     return Quantity; 
    } 
    public void setQuantity(String quantity) { 
     Quantity = quantity; 
    } 

} 

} 

,並在主要活動我得到這樣的數據:

Item[] placelist; 
placelist = gson.fromJson(responseJSON, Item[].class); 
Item item = gson.fromJson(responseJSON, Item.class); 

但我得到了logcat以下例外: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

請問我在做什麼錯?

+0

所以你可以編輯它?因爲我是新的Android和JSON – user3534834

回答

9

看你的代碼,以正確的方式讓你item對象是

Item[] placelist = gson.fromJson(responseJSON, Item[].class);  

,因爲您的JSON是item對象列表[(BEGIN_ARRAY)

Item item = gson.fromJson(responseJSON, Item.class);  

拋出一個異常,因爲GSON是期望單個項目對象{(BEGIN_OBJECT),但是是一個數組。

如果你的JSON是一個數組,將它反序列化爲一個數組,如果你的JSON是一個對象,將它反序列化爲一個對象,但是你不能反序列化相同的JSON,如數組和對象以兩種方式反序列化。

+0

幾乎精確的答案 –

+0

爲我工作.. –

1

您的JSON輸出是一個列表。

// This parses your JSON 
Item[] items = new Gson().fromJson(responseJson, Item[].class); 

在此之後,你就會有項目

+0

好吧,但你看到(_OrderDetails)是一個數組嗎?我怎麼才能得到它? – user3534834

+0

現在它給我NULL POINTER EXCEPTION – user3534834

+0

item.getOrderDetails(); // :) – Toochka

1

的數組實際上,因爲你有對象的數組不是對象。這就是爲什麼第二行工作,第三行不行。所以如果你想將這個json解析爲一個Item []地點列表;你不需要改變任何東西。如果要分析它作爲一個對象,你應該去掉括號這樣的:

{ 
"_OrderDetails": [ 
    { 
    "ProductName": "FUCHS SUPER GT SAE 10W30 6X5/FP10100010102", 
    "TotalAfterDiscount_Lc": "7500", 
    "MeasureUnitName": "كرتونة", 
    "TotalPrice_Lc": "7500", 
    "PricePerUnit_Lc": "75", 
    "Quantity": "100" 
    } 
], 
"Id": "274", 
"OrderDate": "4/10/2014 12:00:00 AM", 
"Number": "16", 
"CustomerName": "الأسد", 
"Note": "" 

}

或解析它像一個數組和檢索的第一個元素。


UPDATE

Item[] placelist = gson.fromJson(responseJSON, Item[].class); 

作品OK,你已經有了項目的數組。但是你有一個OrderDetails名稱的問題。在json中,它是代碼中的「_OrderDetails」,它是「orderDetails」。您可以在字段中添加註釋:

@SerializedName("_OrderDetails") 
    private List<_OrderDetails> orderDetails; 

的完整代碼進行測試:

import com.google.gson.Gson; 
import com.google.gson.annotations.SerializedName; 

import java.util.Arrays; 
import java.util.List; 

public class Main { 
public static void main(String[] args) { 
    String responseJSON = "[\n" + 
      " {\n" + 
      " \"_OrderDetails\": [\n" + 
      "  {\n" + 
      "   \"ProductName\": \"FUCHS SUPER GT SAE 10W30 6X5/FP10100010102\",\n" + 
      "  \"TotalAfterDiscount_Lc\": \"7500\",\n" + 
      "  \"MeasureUnitName\": \"كرتونة\",\n" + 
      "  \"TotalPrice_Lc\": \"7500\",\n" + 
      "  \"PricePerUnit_Lc\": \"75\",\n" + 
      "  \"Quantity\": \"100\"\n" + 
      "  }\n" + 
      " ],\n" + 
      " \"Id\": \"274\",\n" + 
      " \"OrderDate\": \"4/10/2014 12:00:00 AM\",\n" + 
      " \"Number\": \"16\",\n" + 
      " \"CustomerName\": \"الأسد\",\n" + 
      " \"Note\": \"\"\n" + 
      " }\n" + 
      "]"; 

    Item[] placelist; 
    Gson gson = new Gson(); 
    placelist = gson.fromJson(responseJSON, Item[].class); 
    System.out.println(Arrays.toString(placelist)); 
} 


public class Item { 

    private String OrderDate; 
    private String Number; 
    private String Note; 
    private String CustomerName; 
    private String Id; 
    @SerializedName("_OrderDetails") 
    private List<_OrderDetails> orderDetails; 


    public String getOrderDate() { 
     return OrderDate; 
    } 

    public void setOrderDate(String orderDate) { 
     OrderDate = orderDate; 
    } 

    public String getNumber() { 
     return Number; 
    } 

    public void setNumber(String number) { 
     Number = number; 
    } 

    public String getNote() { 
     return Note; 
    } 

    public void setNote(String note) { 
     Note = note; 
    } 

    public String getId() { 
     return Id; 
    } 

    public void setId(String id) { 
     Id = id; 
    } 

    public String getCustomerName() { 
     return CustomerName; 
    } 

    public void setCustomerName(String customerName) { 
     CustomerName = customerName; 
    } 

    public List<_OrderDetails> getOrderDetails() { 
     return orderDetails; 
    } 

    public void setOrderDetails(List<_OrderDetails> orderDetails) { 
     this.orderDetails = orderDetails; 
    } 

    @Override 
    public String toString() { 
     return "Item{" + 
       "OrderDate='" + OrderDate + '\'' + 
       ", Number='" + Number + '\'' + 
       ", Note='" + Note + '\'' + 
       ", CustomerName='" + CustomerName + '\'' + 
       ", Id='" + Id + '\'' + 
       ", orderDetails=" + orderDetails + 
       '}'; 
    } 

    public class _OrderDetails { 
     private String OrderId; 
     private String OrderDate; 
     private String Number; 
     private String Note; 
     private String ProductName; 
     private String TotalAfterDiscount_Lc; 
     private String MeasureUnitName; 
     private String TotalPrice_Lc; 
     private String PricePerUnit_Lc; 
     private String Quantity; 

     public String getOrderId() { 
      return OrderId; 
     } 

     public void setOrderId(String orderId) { 
      OrderId = orderId; 
     } 

     public String getOrderDate() { 
      return OrderDate; 
     } 

     public void setOrderDate(String orderDate) { 
      OrderDate = orderDate; 
     } 

     public String getNumber() { 
      return Number; 
     } 

     public void setNumber(String number) { 
      Number = number; 
     } 

     public String getNote() { 
      return Note; 
     } 

     public void setNote(String note) { 
      Note = note; 
     } 

     public String getProductName() { 
      return ProductName; 
     } 

     public void setProductName(String productName) { 
      ProductName = productName; 
     } 

     public String getTotalAfterDiscount_Lc() { 
      return TotalAfterDiscount_Lc; 
     } 

     public void setTotalAfterDiscount_Lc(String totalAfterDiscount_Lc) { 
      TotalAfterDiscount_Lc = totalAfterDiscount_Lc; 
     } 

     public String getMeasureUnitName() { 
      return MeasureUnitName; 
     } 

     public void setMeasureUnitName(String measureUnitName) { 
      MeasureUnitName = measureUnitName; 
     } 

     public String getTotalPrice_Lc() { 
      return TotalPrice_Lc; 
     } 

     public void setTotalPrice_Lc(String totalPrice_Lc) { 
      TotalPrice_Lc = totalPrice_Lc; 
     } 

     public String getPricePerUnit_Lc() { 
      return PricePerUnit_Lc; 
     } 

     public void setPricePerUnit_Lc(String pricePerUnit_Lc) { 
      PricePerUnit_Lc = pricePerUnit_Lc; 
     } 

     public String getQuantity() { 
      return Quantity; 
     } 

     public void setQuantity(String quantity) { 
      Quantity = quantity; 
     } 

     @Override 
     public String toString() { 
      return "_OrderDetails{" + 
        "OrderId='" + OrderId + '\'' + 
        ", OrderDate='" + OrderDate + '\'' + 
        ", Number='" + Number + '\'' + 
        ", Note='" + Note + '\'' + 
        ", ProductName='" + ProductName + '\'' + 
        ", TotalAfterDiscount_Lc='" + TotalAfterDiscount_Lc + '\'' + 
        ", MeasureUnitName='" + MeasureUnitName + '\'' + 
        ", TotalPrice_Lc='" + TotalPrice_Lc + '\'' + 
        ", PricePerUnit_Lc='" + PricePerUnit_Lc + '\'' + 
        ", Quantity='" + Quantity + '\'' + 
        '}'; 
     } 
    } 

} 

}

+0

我想檢索他們所有的數組和_OrderDetails作爲另一個數組..我請我怎麼做?因爲網絡服務給我的結果,我不能改變它 – user3534834

+0

@ user3534834我編輯了我的答案。 – Moses

+0

我仍然沒有得到..我不知道爲什麼! – user3534834

0

BTW這個JSON的解析是錯誤的。您的訂單詳細信息列表每次都將爲空。

「_OrderDetails」:[...]在JSON意味着訂單的該列表的名稱是 「_OrderDetails」

2

這裏是你如何定義你的類:

public class Item implements Serializable { 
    @SerializedName("_OrderDetails") 
    private OrderDetails[] mOrderDetails; 

    @SerializedName("Id") 
    private String mId; 

    @SerializedName("OrderData") 
    private String mOrderDate; 

    @SerializedName("Number") 
    private String mNumber; 

    @SerializedName("CustomerName") 
    private String mCustomerName; 

    @SerializedName("Note") 
    private String mNote; 

    // Add setters and getters 

    public static class OrderDetails implements Serializable { 
     @SerializedName("ProductName") 
     private String mProductName; 

     @SerializedName("TotalAfterDiscount_Lc") 
     private String mTotalAfterDiscount; 

     @SerializedName("MeasureUnitName") 
     private String mMeasureUnitName; 

     @SerializedName("TotalPrice_Lc" 
     private String mTotalPrice; 

     @SerializedName("PricePerUnit_Lc") 
     private String mPricePerUnit; 

     @SerializedName("Quantity") 
     private String mQuantity; 

     // Add setters and getters 
    } 
} 

編輯。通過下面的代碼片段,您可以將JSON反序列化爲Item對象。

Gson gson = new Gson(); 
Item item = gson.fromJson(json, Item.class); 
+0

好,那麼我怎麼能在主要活動中調用它?如果你不介意要求 – user3534834

+0

..你在這裏? – user3534834

+0

please .. help:'( – user3534834

相關問題