使用GSON将Json解析为Json元素可能不同的JAVA对象 [英] Using GSON to parse Json into a JAVA Object where the Json Elements may vary

查看:110
本文介绍了使用GSON将Json解析为Json元素可能不同的JAVA对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GSON将JSON .txt文件解析为JAVA对象。 JSON文件具有以下结构:

  {
event0:{
a: abc,
b:def
},

event1:{
a:ghi,
b:jkl,
c:mno
}
}

我已将文本文件读入名为dataStr的字符串。我想使用fromJson方法将事件捕获到以下JAVA类中:

  public class Event {
private字符串a;
私人字符串b;
私人字符串c;

public Event(){}

}



<问题是JSON在一些元素中可能有一个额外的字段c。我想将所有事件解析为Event类对象,对于没有c字段的情况,我想在对象中使其为零或零。事先不知道哪些元素具有c字段。


具体来说,我无法弄清楚如何在一些JSON元素中处理一个额外的字段。我想做一些事情:

  Gson gson = new Gson(); 
ArrayList< Event> events = gson.fromJson(dataStr,Event.class);

但是我首先遇到了如何遍历Json文件中的事件,其次,如何处理一些偶尔丢失的字段到同一个Event对象中。我真的很感激一个正确的方向。谢谢大家。



我对JSON解析相当陌生,可能错过了以下答案:

使用Gson将Json转换为Java对象 p>

使用Gson将JSON映射到POJO



使用gson将json解析为java对象



将JSON解析为一个java对象



如何使用GSON将json文件解析为Java POJO类

解决方案

我不确定我是否理解你的问题的权利。根据我的理解,你正试图用一个额外的字段来转换一个json对象,而这个字段在java类中是不可用的。坦率地说,我不明白你为什么要这样做,或者如果可以开始的话。您可以通过将json转换为Map来解决这个问题。



Map map = gson.fromJson(jsonString,Map.class);

I am trying to parse a JSON .txt file into a JAVA object using GSON. The JSON file has the following structure:

    {
        "event0" : {
        "a" : "abc",
        "b" : "def"
        },

        "event1" : {
        "a" : "ghi",
        "b" : "jkl",
        "c" : "mno"
        }
    }

I have read the text file into a String called dataStr. I want to use the fromJson method to capture the events into the following JAVA class:

public class Event {
    private String a;
    private String b;
    private String c;

    public Event() {}

}

The problem is that the JSON might have one extra field "c" in some of the elements. I want to parse all the events into Event class objects, and for the cases where there is no "c" field, I want to make it null or zero in my object. It is not known beforehand which of the elements will have the "c" field.

Specifically, I was not able to figure out how to handle one extra field in some of the JSON elements. I want to do something along the lines of:

Gson gson = new Gson();
ArrayList<Event> events = gson.fromJson(dataStr, Event.class);

But I am stuck with first, how to iterate over the events in the Json file, and secondly, how to handle some occasional missing fields into the same Event object. I would really appreciate a kick in the right direction. Thank you all.

I am fairly new to JSON parsing, and might have missed something in the following answers:

Using Gson to convert Json into Java Object

Mapping JSON into POJO using Gson

Using gson to parse json to java object

Parse JSON into a java object

How to parse a json file into a java POJO class using GSON

解决方案

I'm not sure if I understood your question right. As per my understanding, you are trying to convert a json object with an extra field which is not available in the java class. Frankly, I don't understand why you want that or if it's possible to start with. You can have a workaround by converting the json to Map.

Map map = gson.fromJson(jsonString, Map.class);

这篇关于使用GSON将Json解析为Json元素可能不同的JAVA对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆