解析包含变化键的嵌套JSON对象

huangapple go评论75阅读模式
英文:

Parsing a nested JSON object containing changing keys

问题

我正在使用 Java 中的 Gson 解析 JSON 文件。问题是 JSON 对象包含另一组对象。如果只是这样的话,我可以创建另一个类并进行输入,这不会成为问题。问题是这些子对象包含的键在每个对象中可能不同。我不确定如何处理这种情况。

以下是 JSON 的一部分内容:

[
  {
    "a": "aa",
    "b": 1,
    "c": "cc",
    "d": 2.2,
    "e": 3.3,
    "obj1": {"lu":1,"lo":1},
    "arr1": [],
    "obj2": {}
  },
  {
    "a": "aaa",
    "b": 4,
    "c": "ccc",
    "d": 5.5,
    "e": 6.6,
    "obj1": {"bli":1},
    "arr1": ["Some String"],
    "obj2": {
      "foo": 100,
      "bar": 50,
      "bla": 1
    }
  },
  {
    "a": "aaaa",
    "b": 7,
    "c": "cccc",
    "d": 8.8,
    "e": 9.9,
    "obj1": {},
    "arr1": ["Some String", "Another one"],
    "obj2": {
      "la": 300,
      "le": 20000
    }
  }
]

正如您所见,obj1 和 obj2 包含的键是不一致的。

这是我用来解析数据的 jObject 类的当前代码:

public class jObject {
    String a;
    Integer b;
    String c;
    Double d;
    Double e;
    List<String> arr1;
    
    public jObject(String a, Integer b, String c, Double d, Double e,
                   List<Pair<String, Integer>> obj1, List<String> arr1, List<Pair<String, Integer>> obj2) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.arr1 = arr1;
    }
}

对于所有普通键以及 "arr1" 数组,它都可以正常工作。

以下是实际的解析代码:

Gson gson = new Gson();

Reader reader = Files.newBufferedReader(Paths.get("PATH_TO_JSON"));
List<jObject> objects = gson.fromJson(reader, new TypeToken<List<jObject>>() {}.getType());

我想要实现的目标是,"obj1" 和 "obj2" 将作为 jObject 类的一部分保存为 String 和 Integer 的元组列表。

我已经搜索了相当长时间,但没有找到与这个确切问题相关的内容。谢谢提前帮助!

英文:

I am using Gson in java to parse a JSON file. The problem is that the json objects contain another set of objects. That wouldnt be a problem as I could just create another class and input into that. The problem is that these sub-objects include keys that can be different in each of them. I am unsure how to handle that.
Here's a cutout of the JSON:

[
  {
    &quot;a&quot;: &quot;aa&quot;,
    &quot;b&quot;: 1,
    &quot;c&quot;: &quot;cc&quot;,
    &quot;d&quot;: 2.2,
    &quot;e&quot;: 3.3,
    &quot;obj1&quot;: {&quot;lu&quot;:1,&quot;lo&quot;:1},
    &quot;arr1&quot;: [],
    &quot;obj2&quot;: {}
  },
   {
    &quot;a&quot;: &quot;aaa&quot;,
    &quot;b&quot;: 4,
    &quot;c&quot;: &quot;ccc&quot;,
    &quot;d&quot;: 5.5,
    &quot;e&quot;: 6.6,
    &quot;obj1&quot;: {&quot;bli&quot;:1},
    &quot;arr1&quot;: [&quot;Some String&quot;],
    &quot;obj2&quot;: {
      &quot;foo&quot;: 100,
      &quot;bar&quot;: 50,
      &quot;bla&quot;: 1
    }
  },
   {
    &quot;a&quot;: &quot;aaaa&quot;,
    &quot;b&quot;: 7,
    &quot;c&quot;: &quot;cccc&quot;,
    &quot;d&quot;: 8.8,
    &quot;e&quot;: 9.9,
    &quot;obj1&quot;: {},
    &quot;arr1&quot;: [&quot;Some String&quot;, &quot;Another one&quot;],
    &quot;obj2&quot;: {
      &quot;la&quot;: 300,
      &quot;le&quot;: 20000
    }
  }
]

As you can see, obj1 and obj2 contain keys that are not consistent.
Here is my current code for the jObject class I parse that data into:

public class jObject {
	String a;
	Integer b;
	String c;
	Double d;
	Double e;
//	List&lt;Pair&lt;String,Integer&gt;&gt; obj1;
	List&lt;String&gt; arr1;
//	List&lt;Pair&lt;String,Integer&gt;&gt; obj2;
	
	public jObject(String a, Integer b, String c, Double d, Double e,
				   List&lt;Pair&lt;String,Integer&gt;&gt; obj1, List&lt;String&gt; arr1,List&lt;Pair&lt;String,Integer&gt;&gt; obj2) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;
		
	//	this.obj1 = obj1;
		this.arr1 = arr1;
	//	this.obj2 = obj2;
				   }
}

It works for all the normal keys aswell as the "arr1" array.

Here is how I actually parse it if you want to know:

Gson gson = new Gson();

Reader reader = Files.newBufferedReader(Paths.get(&quot;PATH_TO_JSON&quot;));
List&lt;jObject&gt; objects = gson.fromJson(reader, new TypeToken&lt;List&lt;jObject&gt;&gt;() {}.getType());

What I want to achieve is that "obj1" and "obj2" will be saved as part of the jObject class as Lists of a tuple of String and Integer.

I searched around for quite a bit but I couldnt find anything related to this exact problem.

Thanks in advance!

答案1

得分: 1

可以使用 Map

public class jObject {
    String a;
    Integer b;
    String c;
    Double d;
    Double e;
    Map<String,Integer> obj1;
    List<String> arr1;
    Map<String,Integer> obj2;

    public jObject(String a, Integer b, String c, Double d, Double e,
                   Map<String,Integer> obj1, List<String> arr1, Map<String,Integer> obj2) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;

        this.obj1 = obj1;
        this.arr1 = arr1;
        this.obj2 = obj2;
    }
}
英文:

You can use Map.

public class jObject {
    String a;
    Integer b;
    String c;
    Double d;
    Double e;
    Map&lt;String,Integer&gt; obj1;
    List&lt;String&gt; arr1;
    Map&lt;String,Integer&gt; obj2;

    public jObject(String a, Integer b, String c, Double d, Double e,
                   Map&lt;String,Integer&gt; obj1, List&lt;String&gt; arr1, Map&lt;String,Integer&gt; obj2) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;

        this.obj1 = obj1;
        this.arr1 = arr1;
        this.obj2 = obj2;
    }
}

答案2

得分: 0

JsonObject无法轻易解析为列表或数组。但可以轻易解析为Map,例如,可以手动将其转换为Map<String,Integer>,然后使用entrySet()方法将其转换为Set<Map.Entry<String,Integer>>。

一种更困难的方法是为特定类型实现自己的反序列化器,例如,一对一列表。

英文:

JsonObject cannot be easily parsed into list or array. But it can be easily parsed to a Map, e.g. Map<String, Integer> that can be manually transformed into Set<Map.Entry<String, Integer>> using entrySet() method.

A more difficult way is to implement your own deserializer for a particular type, e.g. List of pairs.

huangapple
  • 本文由 发表于 2020年9月26日 10:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64073213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定