Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 (little edit)

后端 未结 5 664
灰色年华
灰色年华 2020-12-19 19:43

what I am having here is a web service that gives me the following JSON code :

[
  {
    \"_OrderDetails\": [
      {
         \"ProductName\": \"FUCHS SUPER         


        
5条回答
  •  悲哀的现实
    2020-12-19 20:14

    Actually, because you have an array of objects not an object. And that is why the second line works and the third line doesn't. So if you want to parse this json as an Item[] placelist; you don't need to change anything. If you want to parse it as an Object you should remove brackets like this:

    {
    "_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": ""
    

    }

    Or parse it like an array and retrieve the first element.


    UPDATE

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

    Works OK and you've got an array of Items. But you have a problem with the name of OrderDetails. In json it is a "_OrderDetails" in your code it is "orderDetails". You can add annotation on your field:

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

    Full code to test:

    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 + '\'' +
                        '}';
            }
        }
    
    }
    

    }

提交回复
热议问题