首页 > 解决方案 > 无法使用 gson 解析 json 输入

问题描述

我正在尝试使用 Gson 解析器解析 json 输入文件,但它总是抛出错误

'预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT' 不确定我在这里缺少什么。如果我从 EventRecords 中删除引号(这不是有效的 json)并用 //eventRecords = gson.fromJson(bufferedReader, SNSEventRecords.class); 替换下面的代码,我能够解析它

测试1.json:

{
    "EventRecords": [      
            {
                "eventVersion": "2.0",
                "eventSource": "aws:s3",
                "awsRegion": "us-east-1",
                "eventTime": "2018-05-10T17:10:01.420Z",
                "eventName": "ObjectCreated:Put"
            }
        ]
    }

记录.java

public class Record {

String eventVersion;
String eventSource;
String awsRegion;
String eventTime;
String eventName;

public String getEventVersion() {
    return eventVersion;
}
public void setEventVersion(String eventVersion) {
    this.eventVersion = eventVersion;
}
public String getEventSource() {
    return eventSource;
}
public void setEventSource(String eventSource) {
    this.eventSource = eventSource;
}
public String getAwsRegion() {
    return awsRegion;
}
public void setAwsRegion(String awsRegion) {
    this.awsRegion = awsRegion;
}
public String getEventTime() {
    return eventTime;
}
public void setEventTime(String eventTime) {
    this.eventTime = eventTime;
}
public String getEventName() {
    return eventName;
}
public void setEventName(String eventName) {
    this.eventName = eventName;
}
@Override
public String toString () {
  return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
}
}

SNSEventRecords.java:

 public class SNSEventRecords {
    private List<Record> EventRecords;

    public List<Record> getEventRecords() {
        return EventRecords;
    }

    public void setEventRecords(List<Record> eventRecords) {
        this.EventRecords = eventRecords;
    }

    @Override
      public String toString () {
        return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
      }


}

GsonEncoder.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import com.goo*emphasized text*gle.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.uberops.validator.gson.dto.Record;
import com.uberops.validator.gson.dto.SNSEventRecords;

public class GsonEncoder {
    public void jsonEncoder(String filePath) throws FileNotFoundException {

        Type listType = new TypeToken<List<Record>>(){}.getType();
        Gson gson = new Gson();
        JsonReader reader = new JsonReader(new FileReader(filePath));
        //BufferedReader reader = new BufferedReader(new FileReader(filePath));
        SNSEventRecords eventRecords = gson.fromJson(reader, listType); 
        List<Record> records = eventRecords.getEventRecords();
        System.out.println("\n\nEventRecords\n\n" + records.toString());

    }

    public static void main(String[] args) throws FileNotFoundException {
        GsonEncoder obj2 = new GsonEncoder();

        File f2 = new File("/Users/test/Desktop/test1.json");
        obj2.jsonEncoder(f2.getAbsolutePath());


    }
}

标签: javajsongson

解决方案


尝试设置字段的序列化名称eventRecords匹配 JSON。

@SerializedName("EventRecords") 
private List<Record> eventRecords;

推荐阅读