使用 Java 解析 json 并打印键和值
Using Java to parse json and print key and vaules
我有以下 json,需要一些帮助来阅读 json 和键值对。使用 Java 我怎样才能读出这个?
我正在尝试使用 'org.json.simple.parser.JSONParser',但无法读出键和值。
这是我尝试阅读的项目:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(ds.getDoceboCourseData());
JSONObject jsonObject = (JSONObject) obj;
JSONArray items = (JSONArray) jsonObject.get("items");
Iterator<JSONObject> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (Exception e) {
e.printStackTrace();
}
JSON:
{
"data":{
"items":[
{
"user_id":"111",
"username":"111",
"email":"blah@blah.com",
"first_name":"blah",
"last_name":"blah",
"level":"Learner",
"enrollment_date":"2020-06-26 00:43:49",
"active_from":null,
"active_until":null,
"status":"Completed",
"status_id":"2",
"level_id":"3",
"user_status_id":"1",
"user_status":"Active",
"score_given":"0.00",
"forced_score_given":false,
"date_complete":"2020-06-26 01:04:01"
}
],
"count":10,
"has_more_data":true,
"current_page":1,
"current_page_size":10,
"total_page_count":3,
"total_count":23
},
"version":"1.0.0",
"_links":[
]
}
感谢@Pshemo 的帮助:
public void parsedJSON(String json) {
JSONObject obj = new JSONObject(json).getJSONObject("data");
System.out.printf("Object : " + obj);
JSONArray arr = obj.getJSONArray("items");
for (int i = 0; i < arr.length(); i++) {
String status = arr.getJSONObject(i).getString("status");
String empId = arr.getJSONObject(i).getString("username");
System.out.printf("Id : %s Status : %s %n", empId, status);
}
我有以下 json,需要一些帮助来阅读 json 和键值对。使用 Java 我怎样才能读出这个?
我正在尝试使用 'org.json.simple.parser.JSONParser',但无法读出键和值。
这是我尝试阅读的项目:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(ds.getDoceboCourseData());
JSONObject jsonObject = (JSONObject) obj;
JSONArray items = (JSONArray) jsonObject.get("items");
Iterator<JSONObject> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (Exception e) {
e.printStackTrace();
}
JSON:
{
"data":{
"items":[
{
"user_id":"111",
"username":"111",
"email":"blah@blah.com",
"first_name":"blah",
"last_name":"blah",
"level":"Learner",
"enrollment_date":"2020-06-26 00:43:49",
"active_from":null,
"active_until":null,
"status":"Completed",
"status_id":"2",
"level_id":"3",
"user_status_id":"1",
"user_status":"Active",
"score_given":"0.00",
"forced_score_given":false,
"date_complete":"2020-06-26 01:04:01"
}
],
"count":10,
"has_more_data":true,
"current_page":1,
"current_page_size":10,
"total_page_count":3,
"total_count":23
},
"version":"1.0.0",
"_links":[
]
}
感谢@Pshemo 的帮助:
public void parsedJSON(String json) {
JSONObject obj = new JSONObject(json).getJSONObject("data");
System.out.printf("Object : " + obj);
JSONArray arr = obj.getJSONArray("items");
for (int i = 0; i < arr.length(); i++) {
String status = arr.getJSONObject(i).getString("status");
String empId = arr.getJSONObject(i).getString("username");
System.out.printf("Id : %s Status : %s %n", empId, status);
}