对于这个json文件
{
"Name": "crunchify.com",
"Author": "App Shah",
"Company List": [
"Compnay: eBay",
"Compnay: Paypal",
"Compnay: Google"
]
}
我使用了那些代码
Object obj = parser.parse(new FileReader(
"G:/Contextual Search/balll.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("Name");
String author = (String) jsonObject.get("Author");
JSONArray companyList = (JSONArray) jsonObject.get("Company List");
System.out.println("Name: " + name);
System.out.println("Author: " + author);
System.out.println("\nCompany List:");
Iterator<String> iterator = companyList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
如何解析并获取此关键JSON文件的值?嵌套到嵌套数据的地方。在身体标签下有人和有人有偏好,在偏好下有很多价值。获取价值对我来说太复杂了。我怎样才能获得这些价值请提出任何建议?
{"body":
{"group": "Family",
"season": "Summer",
"person":
{"gender": "Male",
"age": 23.0,
"id": 12258,
"preferences": [
{"rating": 3,
"documentId": "TRECCS-00674898-160",
"tags": ["Romantic", "Seafood", "Family Friendly"]},
{"rating": 2,
"documentId": "TRECCS-00247656-160",
"tags": ["Bar-hopping"]},
{"rating": 3,
"documentId": "TRECCS-00085961-160",
"tags": ["Gourmet Food"]},
{"rating": 4,
"documentId": "TRECCS-00086637-160",
"tags": ["Family Friendly", "Local Food", "Entertainment"]},
{"rating": 4,
"documentId": "TRECCS-00086308-160",
"tags": ["Family Friendly", "Tourism"]},
{"rating": 4,
"documentId": "TRECCS-00086622-160",
"tags": ["Healthy Food",
"Romantic", "Gourmet Food"]},
{"rating": 2,
"documentId": "TRECCS-00809111-160",
"tags": ["Wellness",
"Family Friendly",
"Sport"]},
{"rating": 4,
"documentId": "TRECCS-00086310-160",
"tags": ["Family Friendly", "Sport"]},
{"rating": 4,
"documentId": "TRECCS-00340169-160",
"tags": ["Fashion Bargains",
"Live Music",
"Shopping for accessories",
"Family Friendly"]},
{"rating": 4,
"documentId": "TRECCS-00018110-160",
"tags": ["Healthy Food", "Family Friendly", "Local Food", "Organic Food"]},
{"rating": 2, "documentId": "TRECCS-00085880-160",
"tags": ["Romantic", "Sailing", "Seafood"]},
{"rating": 4, "documentId": "TRECCS-00259825-152", "tags": ["Family Friendly", "Budget Friendly"]}]},
"trip_type": "Holiday", "duration": "Weekend trip",
"location": {"lat": 26.56285, "state": "FL", "id": 210, "lng": -81.94953000000001, "name": "Cape Coral"}},
"candidates": ["TRECCS-00001063-210", "TRECCS-00001069-210", "TRECCS-00001080-210", "TRECCS-00001085-210",
"TRECCS-00001086-210", "TRECCS-00001092-210", "TRECCS-00001102-210", "TRECCS-00001114-210", "TRECCS-00001148-210",
"TRECCS-00056424-210", "TRECCS-00056508-210", "TRECCS-00056589-210", "TRECCS-00056591-210", "TRECCS-00056687-210",
"TRECCS-00056715-210", "TRECCS-00316172-210", "TRECCS-00317050-210", "TRECCS-00552677-210", "TRECCS-00552744-210",
"TRECCS-00552876-210", "TRECCS-00553080-210", "TRECCS-00553240-210", "TRECCS-00553540-210", "TRECCS-00852498-210",
"TRECCS-01452784-210"], "id": 2}
答案 0 :(得分:1)
一个选项总是迭代您在问题中显示的JSON字段。另一个更好的选择是将JSON反序列化为对象。许多JSON库都支持此功能。例如,对于Google's Gson,您的代码看起来像......
class Group {
String group;
String season;
MyPerson person;
}
class Person {
String gender;
double age;
int id;
MyPreference[] preferences;
}
class Preference {
int rating;
String documentId;
String[] tags;
}
然后反序列化JSON ...
String json = "Your JSON ...";
Group group = new Gson().fromJson(json, Group.class);
答案 1 :(得分:0)
这是使用您的JSON
的代码 try {
String responseString = "{\n"
+ " \"Name\": \"crunchify.com\",\n"
+ " \"Author\": \"App Shah\",\n"
+ " \"Company List\": [\n"
+ " \"Compnay: eBay\",\n"
+ " \"Compnay: Paypal\",\n"
+ " \"Compnay: Google\"\n"
+ " ]\n"
+ "}";
JSONParser parser = new JSONParser();
Object obj = parser.parse(new String(responseString));
JSONObject Object_index0 = (JSONObject) obj;
String obj21 = Object_index0.get("Name").toString();
String obj22 = Object_index0.get("Author").toString();
Object obj23 = Object_index0.get("Company List");
System.out.println("Name=>" + obj21 + "\nAuthor=>" + obj22 + "\nCompany List=> " + obj23);
JSONArray lang = (JSONArray) Object_index0.get("Company List");
for (int i = 0; i < lang.size(); i++) {
System.out.println("Company Name=>" + lang.get(i));
}
} catch (ParseException e) {
e.printStackTrace();
}
并添加org.json.simple jar。如有任何更多澄清请咨询 乐于帮助 。谢谢