使用 JSONP 在 json 中导航,是否可以将数字作为 JsonObject 返回?
Navigating though a json using JSONP, can a number be returned as a JsonObject?
我正在编写一个函数,它在给定坐标的 json 中导航。例如:
{
"a": {
"b" : "c"
},
"d": {
...
}
}
如果我打电话给
NavigateThroughJson("a.b", myJsonObject)
我应该得到 "c"
作为输出。我这样做是因为我不能使用反序列化,json 具有任意格式。这是我的功能:
public JsonValue NavigateThroughJson(String coordinates, JsonObject jsonObject) {
JsonObject o = jsonObject;
String[] nodes = coordinates.split("\.");//Splits the dot
for (String node: nodes) {
node = node.replace("\"", ""); //Removes "" from the keys
o = o.getJsonObject(node);
}
return o;
}
问题是当我针对以下 json 尝试此操作时(并调用 NavigateThroughJson("high", jsonAbove)):
{"high":7999.0,"vol":1261.83821469,"buy":7826.01,"last":7884.0,...}
什么都没有 returned,就像 o.getJsonObject(...)
return什么都没有,甚至 null
.
我认为发生这种情况是因为 "high" 指向一个数字而不是像 high: {...}
这样的 json 对象,即使一致的库应该 return [=21] =] 作为具有 Type
Number
的 JsonObject。可以看到,JsonObject
实现了JsonValue
,可以有String
、Number
等类型。参见:https://docs.oracle.com/javaee/7/api/javax/json/JsonValue.html
但是,由于 json 对象也实现了 Map<String, JsonValue>
,例如,我可以在执行 map.get("high")
时获得数字,但我认为这不是正确的方法这样做,如果 "high" 指向另一个 JsonValue
而不是 Number
(例如,它是 json 块 {}
),那么我需要治疗这 JsonValue
作为 Map<String, JsonValue>
,但转换不是最好的做法。
更新:
这个库似乎有一个错误。请记住 json 键是带有“”的字符串,所以如果我尝试:
System.out.println(jsonObject.getJsonObject("high"));
System.out.println("hello?");
它不会打印任何东西,甚至上面的"hello?"
也不行!!!但是,如果我这样做:
System.out.println(jsonObject.getJsonObject("\"high\""));
System.out.println("hello?");
"hello?"
被打印出来了,但是它上面的打印是 "null"
即使我 确定 键 "high"
(带有“”)的存在是因为我之前打印了jsonObject
。
Json有4个主要类型:Object、Array、number和string 这些类型分别对应:java类型中的Map、List、BigDecimal和String。因此,对于每种类型的对象,您必须调用正确的函数:getJsonObject
、getJsonArray
、getJsonNumber
或 getJsonString
。您无法在 Json 编号上获取 Json 对象,因此会导致您的错误。
对于你的情况,你不能反序列化你的 json,所以我向你建议我的解决方案是将它解析为一个 Map,然后循环抛出这个 map 以获得你想要的值。这是一个。
我正在编写一个函数,它在给定坐标的 json 中导航。例如:
{
"a": {
"b" : "c"
},
"d": {
...
}
}
如果我打电话给
NavigateThroughJson("a.b", myJsonObject)
我应该得到 "c"
作为输出。我这样做是因为我不能使用反序列化,json 具有任意格式。这是我的功能:
public JsonValue NavigateThroughJson(String coordinates, JsonObject jsonObject) {
JsonObject o = jsonObject;
String[] nodes = coordinates.split("\.");//Splits the dot
for (String node: nodes) {
node = node.replace("\"", ""); //Removes "" from the keys
o = o.getJsonObject(node);
}
return o;
}
问题是当我针对以下 json 尝试此操作时(并调用 NavigateThroughJson("high", jsonAbove)):
{"high":7999.0,"vol":1261.83821469,"buy":7826.01,"last":7884.0,...}
什么都没有 returned,就像 o.getJsonObject(...)
return什么都没有,甚至 null
.
我认为发生这种情况是因为 "high" 指向一个数字而不是像 high: {...}
这样的 json 对象,即使一致的库应该 return [=21] =] 作为具有 Type
Number
的 JsonObject。可以看到,JsonObject
实现了JsonValue
,可以有String
、Number
等类型。参见:https://docs.oracle.com/javaee/7/api/javax/json/JsonValue.html
但是,由于 json 对象也实现了 Map<String, JsonValue>
,例如,我可以在执行 map.get("high")
时获得数字,但我认为这不是正确的方法这样做,如果 "high" 指向另一个 JsonValue
而不是 Number
(例如,它是 json 块 {}
),那么我需要治疗这 JsonValue
作为 Map<String, JsonValue>
,但转换不是最好的做法。
更新:
这个库似乎有一个错误。请记住 json 键是带有“”的字符串,所以如果我尝试:
System.out.println(jsonObject.getJsonObject("high"));
System.out.println("hello?");
它不会打印任何东西,甚至上面的"hello?"
也不行!!!但是,如果我这样做:
System.out.println(jsonObject.getJsonObject("\"high\""));
System.out.println("hello?");
"hello?"
被打印出来了,但是它上面的打印是 "null"
即使我 确定 键 "high"
(带有“”)的存在是因为我之前打印了jsonObject
。
Json有4个主要类型:Object、Array、number和string 这些类型分别对应:java类型中的Map、List、BigDecimal和String。因此,对于每种类型的对象,您必须调用正确的函数:getJsonObject
、getJsonArray
、getJsonNumber
或 getJsonString
。您无法在 Json 编号上获取 Json 对象,因此会导致您的错误。
对于你的情况,你不能反序列化你的 json,所以我向你建议我的解决方案是将它解析为一个 Map,然后循环抛出这个 map 以获得你想要的值。这是一个