简体   繁体   English

GSON从json文件到对象

[英]GSON from json file to object

how can I parse an object that looks like this with GSON: 我如何使用GSON解析一个看起来像这样的对象:

{ response: 
          { value1: 0, 
            value2: "string", 
            bigjsonObject: {
                 value1b: 0,
                 bigJSONArray: [...]
          }
 }

All of the examples in GSON have less mixed value types, and the docs mention something about how this can screw up GSON deserialization but don't elaborate and still suggest that GSON can map this to an object. GSON中的所有示例都具有较少的混合值类型,并且文档中提到了一些有关如何使其破坏GSON反序列化的内容,但没有详细说明,仍然建议GSON可以将其映射到对象。

My current test using gson.fromJSON(inputstream, myObject.class) returns an object with null values, so it is not mapping them. 我当前使用gson.fromJSON(inputstream, myObject.class)返回一个具有空值的对象,因此它没有映射它们。

myObject.class contains an ArrayList of type bigJSONArray myObject.class包含类型为bigJSONArray的ArrayList

public class myObject {

        private ArrayList<bigObjectModel> bigJSONArray;

        myObject(){};
}

my assumption is that my ArrayList object doesn't have the types it is looking for, or something. 我的假设是我的ArrayList对象没有它正在寻找的类型,或其他东西。 But I am misunderstanding how mapping should work in this case. 但是我误解了在这种情况下映射应该如何工作。

In order to parse 为了解析

{ response: 
      { value1: 0, 
        value2: "string", 
        bigjsonObject: {
             value1b: 0,
             bigJSONArray: [...]
      }
}

You need the container class to be 您需要容器类是

public class myObject {

    private int    value1;
    private String value2;
    private Foo    bigjsonObject;
}

Where the Class Foo is 哪里是Foo

public class Foo {

    private int value1b;
    private ArrayList<bigObjectModel> bigJSONArray
}

You may ommit any field and GSON will just skip it 您可以省略任何字段,GSON会跳过该字段

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

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