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

后端 未结 5 656
灰色年华
灰色年华 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条回答
  •  萌比男神i
    2020-12-19 20:10

    Looking at your code, the right way to get your item objects is

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

    because your JSON is an item object list [ (BEGIN_ARRAY)

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

    throws an exception because Gson is expecting an single item object { (BEGIN_OBJECT) but is an array.

    You can not deserialize the same JSON in two ways, as array and as an object, if your JSON is an array, deserialize it as an array, if you JSON is an object deserialize it as an object but you can not deserialize in both ways.

提交回复
热议问题