Gson如何解析嵌套数组

时间:2018-05-22 17:52:45

标签: java json gson

我从API中收到一个嵌套的String JSON数组,我想创建一个ResultType对象并解析其信息。但是,我不确定如何使用GSON。

字符串是:

[
    [{
        "id": "1",
        "Type": "PERSON",
        "Text": "Bob",
        "phone": "123-456-7894",
        "email": "Bob@gmail.com",
        "company": "Bob's company"
    }],
    [{
        "id": "2",
        "Type": "PERSON",
        "Text": "Sam",
        "phone": "123-775-5597",
        "email": "Sam@gmail.com",
        "company": "Sam's company"
    }]
]

我尝试创建一个包含以下内容的类:

public class ResultType {
        private int id;
        private String Type;
        private String Text;
        private String phone;
        private String email;
        private String company;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getType() {
            return Type;
        }

        public void setType(String type) {
            Type = type;
        }

        public String getText() {
            return Text;
        }

        public void setText(String text) {
            Text = text;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }


}

我尝试在main中执行此操作:

    Type resultListType = new TypeToken<ArrayList<ResultType>>(){}.getType();
    List<ResultType> resultObj = new Gson().fromJson(result, resultListType);

或将其视为main中的数组:

ResultType resultObj[] = new Gson().fromJson(result, ResultType[].class);

但是,对于这两种方式,我都会收到错误

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.Gson.fromJson(Gson.java:922)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:836)
    at com.company.Main.main(Main.java:68)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
    ... 7 more

1 个答案:

答案 0 :(得分:0)

你做的课是对的。您还需要为该JSON阵列数组中的所有对象创建一个ArrayList,

ArrayList<ResultType> all = new ArrayList<ResultType>;

要解析该JSON结果,您可以使用 JSONArray JSONObject 对象。 因为你有一个数组数组(即使每个内部数组都有一个对象),你可以做类似这样的事情

JSONArray result_array= new JSONArray(string_result);
JSONArray internal_array;
JSONObject object;
ResultType temp;

for(int i=0;i<result_array.size();i++){//scan the main array
    internal_array  new JSONArray(result_array.get(i)); //get the i array

    for(int k=0;k<internal_array.size();k++){ //scan the i intern array(with always one element)
          object = new JSONObject(internal_array.get(k));
          temp = new ResultType();
          temp.setId(object.getString("id"));
          temp.setId(object.getString("phone"));
          .
          .
          all.add(temp);
    }
}
相关问题