我想从json对象获取密钥。我使用下面的代码来获取它,
JSONParser parser = new JSONParser();
try {
Object obj = null;
try {
obj = parser.parse(new FileReader(new File("json/customer_list.json")));
} catch (org.json.simple.parser.ParseException e1) {
e1.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
for (int i = 0; i < listOfBranches.size(); i++) {
System.out.println("Customer :" + listOfBranches.get(i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在这部分中,
System.out.println(&#34;客户:&#34; + listOfBranches.get(i));
我得到了下面的对象,
{"Photo":{"input_required":false,"output_type":"","input_fields":[{"input_type":"INTEGER","length":"","reg_exp":"","label":"","field_type":"TEXT"},{"values":"","length":"","reg_exp":"","label":"Mode","field_type":"drop_down"},{"length":"","input_type":"INTEGER","reg_exp":"","label":"Quantity","field_type":"TEXT"}]},"video":{"input_required":true,"output_type":"","input_pattern":""},"drawing":{"input_required":true,"output_type":"PDF","input_pattern":""}}
从这个json我想得到照片,视频和绘图的关键。你能建议我这么做吗?提前谢谢。
答案 0 :(得分:-2)
我使用下面的代码
从json对象获取密钥try {
Object obj = null;
try {
obj = parser.parse(new FileReader(new File("json/customer_list.json")));
} catch (org.json.simple.parser.ParseException e1) {
e1.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
for (int i = 0; i < listOfBranches.size(); i++) {
JSONObject item = (JSONObject)listOfBranches.get(i);
Set keys = item.keySet();
Iterator a = keys.iterator();
while(a.hasNext()) {
String key = (String)a.next();
System.out.print("key : "+key);
}
System.out.println("Customer :" + listOfBranches.get(i)+"\n item "+item);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
快乐编码......