简体   繁体   English

Gson如何解析嵌套数组

[英]Gson How to parse nested arrays

I am receiving a nested String JSON array from an API that I want to create a ResultType object and parse its information. 我从要创建ResultType对象并解析其信息的API接收嵌套的String JSON数组。 However, I am not sure how to doing using GSON. 但是,我不确定如何使用GSON。

The String is: 字符串是:

[
    [{
        "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"
    }]
]

I have tried making a class that contains: 我尝试过制作一个包含以下内容的类:

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;
        }


}

and I have tried doing this in main: 而且我主要尝试这样做:

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

or treating it as an array in main: 或将其视为main中的数组:

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

However, for both ways, I am getting the error 但是,无论哪种方式,我都会遇到错误

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

The class that you made is right. 您所上的课是对的。 You need also to create a ArrayList for all the objects taht you hava in that JSON array of array, 您还需要为该数组的JSON数组中的所有对象创建一个ArrayList,

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

To parse that JSON result you can use JSONArray an JSONObject objects. 要解析该JSON结果,可以使用JSONArrayJSONObject对象。 Because of you have an array of arrays (even if each inside arrays have a single object) you can do something like this 因为您有一个数组数组(即使每个内部数组都有一个对象),您也可以这样做

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);
    }
}

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

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