Java Jackson - 如何解析带有任意标识符标签的对象列表
Java Jackson - how to parse list of objects with an arbatrary identifier tag
我有一个 JSON 记录,如下所示:
{"ActionRecord": {
"101": {
"Desc": "string 1",
"Done": 1,
"MaxTimes": 2,
"Point": 30,
"Times": 4
},
"102": {
"Desc": "string 2",
"Done": 1,
"MaxTimes": 3,
"Point": 15,
"Times": 13
},
"103": {
"Desc": "string 3.",
"Done": 1,
"MaxTimes": 5,
"Point": 15,
"Times": 24
}, ... }
如果我创建一个 hacky 中间体 class,其中包含每个数字的字段,然后在 class:
中使用类似的东西,我可以让 Jackson 解析它
@JsonProperty( value = "101" )
public MyClass hundred_one;
@JsonProperty( value = "102" )
public MyClass hundred_two;
@JsonProperty( value = "103" )
public MyClass hundred_three;
但我必须输入所有预期值,因此使用对象数组列表并使用 Jackson 的映射器将数字 ID 插入 POJO 会容易得多。
有没有办法让 Jackson 自动将其映射成这样的 class? :
public enum ActionRecord {
Something ( "101" ),
SomethingElse( "102" ),
AnotherSomething ( "103" ),
;
String _id;
EK_DailyTaskInfo_ActionRecord( String id )
{
_id = id;
}
public String getId()
{
return _id;
}
public String Desc; // "some string.",
public boolean Done; // 1,
public int Times; // 4
public int MaxTimes; // 2,
public int Point; // 30,
}
它不一定是一个枚举,这只是我在放弃之前尝试过的东西
好吧,我在示例中使用了 GSON
库,Android 有自己的 api 来处理 JSON
,其中迭代器非常有用,代码也是github
可用
我想建议的是,您应该读取与它的键相关的所有键并从那里获取 JsonObject
,您有一个 JsonList,它不是数组,这就是您可以做的
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(result); // result is your json data
JsonObject obj = element.getAsJsonObject();
System.out.println(obj.toString());
JsonObject jsonObject = obj.getAsJsonObject("ActionRecord"); // this will get the JsonObject with the key ActionRecord
System.out.println(jsonObject);
Set<Map.Entry<String, JsonElement>> stringSet = jsonObject.entrySet(); // this will map all the JsonObject with it's keys
for (Map.Entry<String, JsonElement> key :stringSet) {
System.out.println(jsonObject.getAsJsonObject(key.getKey()).toString());
}
一旦您拥有对应的键 JSONObject
,您就可以为该对象创建填充您自己的类型。
嗯,这是给 Gson
的,您可能想在 Jackson
中寻找等效项
output
{"Desc":"string 1","Done":1,"MaxTimes":2,"Point":30,"Times":4}
{"Desc":"string 2","Done":1,"MaxTimes":3,"Point":15,"Times":13}
{"Desc":"string 3.","Done":1,"MaxTimes":5,"Point":15,"Times":24}
Jackson 可以为您将其解码为 Map<String, Record>
,例如
public class Record {
public String Desc; // "some string.",
public boolean Done; // 1,
public int Times; // 4
public int MaxTimes; // 2,
public int Point; // 30,
}
public class ActionRecords {
public Map<String, Record> ActionRecord
}
我有一个 JSON 记录,如下所示:
{"ActionRecord": {
"101": {
"Desc": "string 1",
"Done": 1,
"MaxTimes": 2,
"Point": 30,
"Times": 4
},
"102": {
"Desc": "string 2",
"Done": 1,
"MaxTimes": 3,
"Point": 15,
"Times": 13
},
"103": {
"Desc": "string 3.",
"Done": 1,
"MaxTimes": 5,
"Point": 15,
"Times": 24
}, ... }
如果我创建一个 hacky 中间体 class,其中包含每个数字的字段,然后在 class:
中使用类似的东西,我可以让 Jackson 解析它 @JsonProperty( value = "101" )
public MyClass hundred_one;
@JsonProperty( value = "102" )
public MyClass hundred_two;
@JsonProperty( value = "103" )
public MyClass hundred_three;
但我必须输入所有预期值,因此使用对象数组列表并使用 Jackson 的映射器将数字 ID 插入 POJO 会容易得多。
有没有办法让 Jackson 自动将其映射成这样的 class? :
public enum ActionRecord {
Something ( "101" ),
SomethingElse( "102" ),
AnotherSomething ( "103" ),
;
String _id;
EK_DailyTaskInfo_ActionRecord( String id )
{
_id = id;
}
public String getId()
{
return _id;
}
public String Desc; // "some string.",
public boolean Done; // 1,
public int Times; // 4
public int MaxTimes; // 2,
public int Point; // 30,
}
它不一定是一个枚举,这只是我在放弃之前尝试过的东西
好吧,我在示例中使用了 GSON
库,Android 有自己的 api 来处理 JSON
,其中迭代器非常有用,代码也是github
我想建议的是,您应该读取与它的键相关的所有键并从那里获取 JsonObject
,您有一个 JsonList,它不是数组,这就是您可以做的
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(result); // result is your json data
JsonObject obj = element.getAsJsonObject();
System.out.println(obj.toString());
JsonObject jsonObject = obj.getAsJsonObject("ActionRecord"); // this will get the JsonObject with the key ActionRecord
System.out.println(jsonObject);
Set<Map.Entry<String, JsonElement>> stringSet = jsonObject.entrySet(); // this will map all the JsonObject with it's keys
for (Map.Entry<String, JsonElement> key :stringSet) {
System.out.println(jsonObject.getAsJsonObject(key.getKey()).toString());
}
一旦您拥有对应的键 JSONObject
,您就可以为该对象创建填充您自己的类型。
嗯,这是给 Gson
的,您可能想在 Jackson
output
{"Desc":"string 1","Done":1,"MaxTimes":2,"Point":30,"Times":4}
{"Desc":"string 2","Done":1,"MaxTimes":3,"Point":15,"Times":13}
{"Desc":"string 3.","Done":1,"MaxTimes":5,"Point":15,"Times":24}
Jackson 可以为您将其解码为 Map<String, Record>
,例如
public class Record {
public String Desc; // "some string.",
public boolean Done; // 1,
public int Times; // 4
public int MaxTimes; // 2,
public int Point; // 30,
}
public class ActionRecords {
public Map<String, Record> ActionRecord
}