简体   繁体   English

使用JsonArray和JsonObject读取Json时出现Gson异常

[英]Gson exception while reading Json with JsonArray and JsonObject

I am trying to call a json response using Gson. 我正在尝试使用Gson调用json响应。 But it contains a JSONArray object on one place and a JSONObject in place in the same hierarchy, here my json response is: 但是它在同一位置包含一个JSONArray对象,并且在同一层次结构中包含一个JSONObject,这里我的json响应是:

"{"SERVICES":{"Results":[{"Items":{"Item":[{"@Id":"10"},{"@Id":"12"}]}}, 
  {"Items":{"Item":{"@Id":"13"}}}]}}"

in the structure it is in this hierarchy, 在这个层次结构中

    {"SERVICES":
        {"Results":
            [
             {"Items":
                {"Item":
                    [
                     {"@Id":"10"},
                     {"@Id":"12"}
                    ]
                }
             },
             {"Items":
                {"Item":
                    {"@Id":"13"}
                }
             }
             ]
        }
    }

Here first 'Item' element contain an array and second one is an object. 在这里,第一个“ Item”元素包含一个数组,第二个是一个对象。 Below is my code, 下面是我的代码,

import java.util.Iterator;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class Next { 
public static void main(String args[]){

    String jsonData = Services.getJsonData();   //{"SERVICES":{"Results":[{"Items":{"Item":[{"@Id":"10"},{"@Id":"12"}]}}, {"Items":{"Item":{"@Id":"13"}}}]}}    

    TResponseInfo responseInfo = new Gson().fromJson(jsonData, TResponseInfo.class);        
    }

class TResponseInfo{
    TServicesInfo SERVICES;
}

class TServicesInfo {
    List<TResultsInfo> Results;
}

class TResultsInfo {
    TItemsInfo Items;
}

class TItemsInfo {
    List<TItemInfo> Item;
    //TItemInfo Item;
}

class TItemInfo {
    @SerializedName("@Id")
    int Id;
}

Here I am getting the exception: 在这里,我得到了例外:

failed to deserialize json object {"@Id":"13"} given the type java.util.List 给定类型java.util.List,无法反序列化json对象{“ @Id”:“ 13”}

and message: This is not a JSON Array. 和消息:这不是JSON数组。

Update: Your Json seem to be wrong, your Item: is an array, but the seconds field in your JSON is an object. 更新:您的Json似乎是错误的,您的Item:是一个数组,但是JSON中的seconds字段是一个对象。

First you have: 首先,您有:

"Item":
                [
                 {"@Id":"10"},
                 {"@Id":"12"}
                ]

then you have: 那么你有:

"Item":
                {"@Id":"13"}

This should be: 应该是:

 "Item":
               [
                {"@Id":"13"}
               ]

It can be hard to manage generics with Gson. 使用Gson可能很难管理泛型。 I would suggest you to use arrays instead. 我建议您改用数组。 Eg something like this: 例如这样的事情:

class TResponseInfo{
    TServicesInfo SERVICES;
}

class TServicesInfo {
    TResultsInfo[] Results; // an array  instead of a generic List
}

class TResultsInfo {
    TItemsInfo Items;
}

class TItemsInfo {
    TItemInfo[] Item; // an array instead of a generic List
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM