我想从这个 JSONObject Data 中获取字段“_source”的内容,
JSONObject jsonObj =
{
"hits" : [
{
"_index" : "try1",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"target" : {
"br_id" : 0,
"wo_id" : 2,
"process" : [
"element 1",
"element 2"
]
},
"explanation" : {
"an_id" : 1311,
"pa_name" : "micha"
},
"text" : "hello world"
}
}
]
}
结果应该如下所示,
String result =
{
"target" : {
"br_id" : 0,
"wo_id" : 2,
"process" : [
"element 1",
"element 2"
]
},
"explanation" : {
"an_id" : 1311,
"pa_name" : "micha"
},
"text" : "hello world"
}
我尝试了这个,但它总是无法识别“_source”字段,
JSONObject main = jsonObj.getJSONObject("hits");
JSONObject content = main.getJSONObject("_source");
JSONObject field = content.getJSONObject("target");
JSONObject["_source"] not found
JSONObject["target"] not found
请提出任何建议或建议,以便我可以从“_source”获取内容作为结果? 谢谢。
最佳答案
_source
位于 JSONArray hits
内部,因此首先获取 JSONArray
JSONArray main = jsonObj.getJSONArray("hits");
然后从数组中获取第一个JSONObject
JSONObject obj = main.getJSONObject(0);
JSONObject source = obj.getJSONObject("_source"); //now get the _source
关于java - 如何从 JSONObject 数据中获取特定字段的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57038746/