我想问一下是否有人知道如何检查Java中JSON值的类型?
例如,如果我们有以下JSON文件:
{
"Name": {
"First": "Job",
"Last": "MOUN"
},
"phoneNumbers": [
{
"location": "home",
"code": 44
}
],
"age": 16
}
然后我想知道"Name"
的例子,类型是一个对象,而"phoneNumbers"
是一个数组。
答案 0 :(得分:0)
这取决于你使用的是什么包。我很喜欢javax.json package,
以下是如何使用它的示例:
//convert String to JsonObject
JsonObject object= Json.createReader(new StringReader(myString)).readObject();
//get the value of "Name"
JsonValue myvalue=object.get("Name");
//print the type
System.out.println(myvalue.getValueType()) ; // in this case it prints JsonObject;