代码之家  ›  专栏  ›  技术社区  ›  nad87563

无法使用gson分析json输入

  •  0
  • nad87563  · 技术社区  · 8 年前

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

    '应为BEGIN\u数组,但在第1行第2列路径$处为BEGIN\u对象' 我不确定我错过了什么。如果我从EventRecords(这不是有效的json)中删除引号并用//EventRecords=gson替换下面的代码,我就能够解析它。fromJson(bufferedReader,snsevenRecords.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());
    
    
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Jose Martinez    8 年前

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

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