英文:
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:
[
{
"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
}
}
]
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<Pair<String,Integer>> obj1;
List<String> arr1;
// List<Pair<String,Integer>> obj2;
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.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("PATH_TO_JSON"));
List<jObject> objects = gson.fromJson(reader, new TypeToken<List<jObject>>() {}.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<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;
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论