enter Access.Application oAccess = null;
// Start a new instance of Access for Automation:
oAccess = new Access.Application();
// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(@"C:\Users\me\documents\visual studio 2017\Projects\dbapp\dbapp\mydb.accdb", true);
oAccess.Visible = true;`
以上是我从ji返回的json
{
"total_hits": 20277,
"max_score": 10.676512,
"hits": [
{
"_id": "513fceb375b8dbbc21000022",
"fields": {
"item_name": "Cheddar Cheese - 1 cup, diced",
"brand_name": "USDA",
"nf_calories": 531.96,
"nf_total_fat": 43.74
}
},
{
"_id": "513fceb375b8dbbc2100001d",
"fields": {
"item_name": "Cheddar Cheese - 1 slice (1 oz)",
"brand_name": "USDA",
"nf_calories": 112.84,
"nf_total_fat": 9.28
}
}
]
}
我正在返回一个JSON对象"命中"使用附加的代码但我想要做的是访问"字段"在这个对象的数组,但我一直无法做到这一点 任何帮助都很高兴
提前致谢
答案 0 :(得分:0)
以下是一些示例代码,用于说明如何使用org.json
API:
JSONObject raintopLevel = new JSONObject(rainbuilder.toString());
JSONArray hits = raintoplevel.getJSONArray("hits");
JSONObject obj = hits.getJSONObject(0); // first object in array
String id = obj.getString("_id");
JSONObject obj2 = obj.getJSONObject("fields");
String itemName = obj2.getString("item_name");
答案 1 :(得分:0)
"命中"是一个JSONArray,所以你必须使用JSONArray
JSONArray raintopLevel = rainbuilder.getJSONArray("hits");
然后你可以迭代数组
for (int i = 0; i < raintopLevel.length(); i++) {
JSONObject json_hits = raintopLevel.getJSONObject(i);
String id = json_id.getString("_id");
JSONArray ja_fields= json_id.getJSONObject("fields");
String item_name = ja_fields.getString("item_name");
}
答案 2 :(得分:0)
“hits”不是JSONObject,而是JSONArray。 所以你需要使用JSONArray。 您可以使用以下代码。
JSONObject raintopLevel = new JSONObject(rainbuilder.toString());
JSONArray hits = raintoplevel.getJSONArray("hits");
JSONObject object11 = hits.getJSONObject(0); // for "hits"
String id = object11.getString("_id");
JSONObject object22 = object11.getJSONObject("fields"); // for "fields" that is inside "hits"
String item_name = object22.getString("item_name");
String brand_name = object22.getString("brand_name");