将未标记的JSON String解析为java对象

时间:2018-02-14 12:56:08

标签: java json

我有一个JSON字符串,我必须解析为java对象。挑战是没有标记值,并且还有一个没有标记每组值的集合。请协助或参考类似的例子。

{
"body": {
    "records": [
        ["567567/0001", "1499669323", "2806", "676768775", "258596", [
            ["collection/Coin", "AZR", "456.65", "0"]
        ]],
        ["456456/0001", "1499669327", "2806", "6767687565", "252256", [
            ["collection", "AZR", "3940.00", "13"],
            ["collection/Coin", "AZR", "89.63", "0"]
        ]],
        ["354645/0001", "1499669330", "2806", "6767687878", "25452212", [
            ["collection", "AZR", "3940.00", "13"],
            ["collection/Coin", "AZR", "125.36", "0"]
        ]],
        ["4654/0001", "1499669333", "2806", "676768787", "2514785", [
            ["collection", "AZR", "4560.00", "21"],
            ["collection/Coin", "AZR", "987.25", "0"]
        ]]
    ]
},
"responseCode": 0,
"responseMsg": "OK"
}

我想要解析JSON字符串的类在

之下
public class Record {
    private String cdmId;
    private Integer transactionNumber;
    private String collectorUserNumber;
    private String collectionStartTime;
    private String sealNumber;

    private List<CollectionType> collection;

    // getters and setters
}

CollectionType类

public class CollectionType {

    private String type;
    private String currency;
    private Double totalAmount;
    private Integer totalPcs;

    // getters and setters
}

1 个答案:

答案 0 :(得分:0)

在GSON中,您有三种选择如何解析对象,如UserGuide中所述。

  
      
  1. 使用Gson的解析器API(低级流解析器或DOM解析器JsonParser)来解析数组元素然后使用   每个数组元素都有Gson.fromJson()。这是首选   做法。这是一个演示如何执行此操作的示例。

  2.   
  3. 为Collection.class注册一个类型适配器,它查看每个数组成员并将它们映射到适当的对象。该   这种方法的缺点是它会搞砸反序列化   其他收集类型在Gson。

  4.   
  5. 为MyCollectionMemberType注册一个类型适配器,并使用fromJson()和Collection。

  6.   

我实施了选项3.自定义TypeAdapter

消息类包含整个消息。

public class Message {
    private Body body;
    private int responseCode;
    private String responseMsg;

    // getters and setters
}

Body class 包含记录

public class Body {
    private List<Record> records;

    public Body() {
        records = new ArrayList<Record>();
    }

    public void addRecod(Record record) {
        records.add(record);
    }

    public List<Record> getRecords() {
        return records;
    }
}

Record和CollectionType类与您发布的相同。

RecordAdapter类来解析自定义集合。

public class RecordAdapter extends TypeAdapter<Record> {

    @Override
    public Record read(JsonReader reader) throws IOException {
        if (reader.peek() == JsonToken.NULL) {
             reader.nextNull();
             return null;
        }
        Record record = new Record();
        // read array representing a record
        reader.beginArray();
        record.setCdmId(reader.nextString());
        record.setTransactionNumber(Integer.valueOf(reader.nextString()));
        record.setCollectorUserNumber(reader.nextString());
        record.setCollectionStartTime(reader.nextString());
        record.setSealNumber(reader.nextString());
        // read array containing CollectionType objects
        reader.beginArray();
        while(reader.hasNext()) {
            // read array containing single CollectionTyoe object
            reader.beginArray();
            CollectionType collectionType = new CollectionType();
            collectionType.setType(reader.nextString());
            collectionType.setCurrency(reader.nextString());
            collectionType.setTotalAmount(reader.nextDouble());
            collectionType.setTotalPcs(reader.nextInt());
            record.addCollectionType(collectionType);
            // read end of array containing single CollectionTyoe object
            reader.endArray();
        }
        // read end of array containing CollectionType
        reader.endArray();
        // read end of array representing a record
        reader.endArray();
        return record;
    }

    @Override
    public void write(JsonWriter arg0, Record arg1) throws IOException {
        // TODO Auto-generated method stub

    }

}

主要方法试用

public static void main(String[] args) {
    String input = "<Your JSON string goes here>";
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Record.class, new RecordAdapter());
    Gson gson = builder.create();
    Message message = gson.fromJson(input, Message.class);
    System.out.println(message);
}

GSON依赖

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.2</version>
  <scope>compile</scope>
</dependency>