无法使用gson解析json输入

时间:2018-05-16 19:17:11

标签: java json gson

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

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

test1.json:

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

Record.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());


    }
}

1 个答案:

答案 0 :(得分:2)

尝试设置eventRecords字段的serialized name以匹配JSON。

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