简体   繁体   English

无法使用gson解析json输入

[英]Unable to parse json input using gson

I am trying to parse json input file using Gson parser but it always throws error 我正在尝试使用Gson解析器解析json输入文件,但始终会引发错误

'Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $' Not sure what i am missing here. “预期BEGIN_ARRAY但在第1行第2列路径$是BEGIN_OBJECT”'不知道我在这里缺少什么。 I am able to parse it if i remove the quotes from EventRecords(which is not a valid json) and replace the below code with //eventRecords = gson.fromJson(bufferedReader, SNSEventRecords.class); 如果我从EventRecords(不是有效的json)中删除引号并将下面的代码替换为// eventRecords = gson.fromJson(bufferedReader,SNSEventRecords.class),则可以解析它。

test1.json: 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 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: 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: 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());


    }
}

Try setting the serialized name of the eventRecords field to match the JSON. 尝试设置eventRecords字段的序列化名称以匹配JSON。

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

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

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