如何将 JSON 数组转换为 JSON 对象
How to convert JSON array into JSON object
我有以下 JSON 数据:
[
{
"sendingReqDataSet": {
"totalCount": 1,
"limit": 50,
"offset": 0,
"status_code": true,
"contacts": [
{
"id": 1,
"request_to": "Shehla_kiran",
"request_by": "ayesha",
"product_name": "Dead Space",
"schedule_date_time": "2016-09-21 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 2,
"request_to": "muqadas",
"request_by": "muqadas",
"product_name": "battleField",
"schedule_date_time": "2016-09-22 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 3,
"request_to": "Shehla_kiran",
"request_by": "Shehla_kiran",
"product_name": "Mario",
"schedule_date_time": "2016-09-12 00:00:00",
"place": "Lahore",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 4,
"request_to": "Shehla_kiran",
"request_by": "Mari",
"product_name": "Mario",
"schedule_date_time": "0000-00-00 00:00:00",
"place": "",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 5,
"request_to": "Shehla_kiran",
"request_by": "Faisal Ali",
"product_name": "Dead Space",
"schedule_date_time": "2016-10-28 00:00:00",
"place": "lahore",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 6,
"request_to": "muqadas",
"request_by": "Faisal Ali",
"product_name": "battleField",
"schedule_date_time": "2016-10-25 00:00:00",
"place": "Canal",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 7,
"request_to": "Shehla_kiran",
"request_by": "Shehla_kiran",
"product_name": "Dead Space",
"schedule_date_time": "2016-10-17 00:00:00",
"place": "Lahore",
"request_type": "Swap",
"status": "Pending"
}
]
}
}
]
我试图在列表视图中显示它,但是当我尝试解析它并在列表视图中显示时,出现以下错误:
W/System.err: org.json.JSONException:
Value [{"sendingReqDataSet":{"totalCount":1,"limit":50,"offset":0,"status_code":true,"contacts":[{"id":1,"request_to":"Shehla_kiran","request_by":"ayesha","product_name":"Dead Space","schedule_date_time":"2016-09-21 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":2,"request_to":"muqadas","request_by":"muqadas","product_name":"battleField","schedule_date_time":"2016-09-22 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":3,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Mario","schedule_date_time":"2016-09-12 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"},{"id":4,"request_to":"Shehla_kiran","request_by":"Mari","product_name":"Mario","schedule_date_time":"0000-00-00 00:00:00","place":"","request_type":"Swap","status":"Pending"},{"id":5,"request_to":"Shehla_kiran","request_by":"Faisal Ali","product_name":"Dead Space","schedule_date_time":"2016-10-28 00:00:00","place":"lahore","request_type":"Swap","status":"Pending"},{"id":6,"request_to":"muqadas","request_by":"Faisal Ali","product_name":"battleField","schedule_date_time":"2016-10-25 00:00:00","place":"Canal","request_type":"Swap","status":"Pending"},{"id":7,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Dead Space","schedule_date_time":"2016-10-17 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"}]}}]
of type org.json.JSONArray cannot be converted to JSONObject
目前,我正在这样做:
try {
// jsonObject = new JSONObject();
JSONObject jsonObject = new JSONObject(json_string);
// JSONArray query = jsonParse.getJSONArray("courses");
jsonArray = jsonObject.getJSONArray("contacts");
int count = 0;
String name, email, moblile;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
name = JO.getString("request_to");
email = JO.getString("request_by");
moblile = JO.getString("product_name");
Contacts contacts = new Contacts(name, email, moblile);
contactAdapter.add(contacts);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray array = new JSONArray(json_string);
JSONObject obj = array.getObject("sendingReqDataSet");
jsonArray = obj.getJSONArray("contacts");
您可以使用以下代码获取对象:
jsonArray = jsonObject.getJSONArray("contacts");
for(int i=0; i<jsonArray.size(); i++) {
JSONObject jobject = jsonArray.getJSONObject(i)
}
使用 Gson 库(https://github.com/google/gson)
库支持转换为 JSON 或从 JSON 转换为对象。
String data = /*load the json data*/
try {
JSONArray jsonArray = new JSONArray(data);
JSONObject object = jsonArray.getJSONObject(0);
JSONObject sendingReqDataSetObject = object.getJSONObject("sendingReqDataSet");
JSONArray arrayContacts = sendingReqDataSetObject.getJSONArray("contacts");
for (int i = 0; i<arrayContacts.length(); i++) {
JSONObject contactObject = arrayContacts.getJSONObject(i);
System.out.println(contactObject.getString("status"));
}
} catch (JSONException e) {
e.printStackTrace();
}
我有以下 JSON 数据:
[
{
"sendingReqDataSet": {
"totalCount": 1,
"limit": 50,
"offset": 0,
"status_code": true,
"contacts": [
{
"id": 1,
"request_to": "Shehla_kiran",
"request_by": "ayesha",
"product_name": "Dead Space",
"schedule_date_time": "2016-09-21 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 2,
"request_to": "muqadas",
"request_by": "muqadas",
"product_name": "battleField",
"schedule_date_time": "2016-09-22 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 3,
"request_to": "Shehla_kiran",
"request_by": "Shehla_kiran",
"product_name": "Mario",
"schedule_date_time": "2016-09-12 00:00:00",
"place": "Lahore",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 4,
"request_to": "Shehla_kiran",
"request_by": "Mari",
"product_name": "Mario",
"schedule_date_time": "0000-00-00 00:00:00",
"place": "",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 5,
"request_to": "Shehla_kiran",
"request_by": "Faisal Ali",
"product_name": "Dead Space",
"schedule_date_time": "2016-10-28 00:00:00",
"place": "lahore",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 6,
"request_to": "muqadas",
"request_by": "Faisal Ali",
"product_name": "battleField",
"schedule_date_time": "2016-10-25 00:00:00",
"place": "Canal",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 7,
"request_to": "Shehla_kiran",
"request_by": "Shehla_kiran",
"product_name": "Dead Space",
"schedule_date_time": "2016-10-17 00:00:00",
"place": "Lahore",
"request_type": "Swap",
"status": "Pending"
}
]
}
}
]
我试图在列表视图中显示它,但是当我尝试解析它并在列表视图中显示时,出现以下错误:
W/System.err: org.json.JSONException:
Value [{"sendingReqDataSet":{"totalCount":1,"limit":50,"offset":0,"status_code":true,"contacts":[{"id":1,"request_to":"Shehla_kiran","request_by":"ayesha","product_name":"Dead Space","schedule_date_time":"2016-09-21 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":2,"request_to":"muqadas","request_by":"muqadas","product_name":"battleField","schedule_date_time":"2016-09-22 00:00:00","place":"lhr","request_type":"Swap","status":"Pending"},{"id":3,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Mario","schedule_date_time":"2016-09-12 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"},{"id":4,"request_to":"Shehla_kiran","request_by":"Mari","product_name":"Mario","schedule_date_time":"0000-00-00 00:00:00","place":"","request_type":"Swap","status":"Pending"},{"id":5,"request_to":"Shehla_kiran","request_by":"Faisal Ali","product_name":"Dead Space","schedule_date_time":"2016-10-28 00:00:00","place":"lahore","request_type":"Swap","status":"Pending"},{"id":6,"request_to":"muqadas","request_by":"Faisal Ali","product_name":"battleField","schedule_date_time":"2016-10-25 00:00:00","place":"Canal","request_type":"Swap","status":"Pending"},{"id":7,"request_to":"Shehla_kiran","request_by":"Shehla_kiran","product_name":"Dead Space","schedule_date_time":"2016-10-17 00:00:00","place":"Lahore","request_type":"Swap","status":"Pending"}]}}]
of type org.json.JSONArray cannot be converted to JSONObject
目前,我正在这样做:
try {
// jsonObject = new JSONObject();
JSONObject jsonObject = new JSONObject(json_string);
// JSONArray query = jsonParse.getJSONArray("courses");
jsonArray = jsonObject.getJSONArray("contacts");
int count = 0;
String name, email, moblile;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
name = JO.getString("request_to");
email = JO.getString("request_by");
moblile = JO.getString("product_name");
Contacts contacts = new Contacts(name, email, moblile);
contactAdapter.add(contacts);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray array = new JSONArray(json_string);
JSONObject obj = array.getObject("sendingReqDataSet");
jsonArray = obj.getJSONArray("contacts");
您可以使用以下代码获取对象:
jsonArray = jsonObject.getJSONArray("contacts");
for(int i=0; i<jsonArray.size(); i++) {
JSONObject jobject = jsonArray.getJSONObject(i)
}
使用 Gson 库(https://github.com/google/gson)
库支持转换为 JSON 或从 JSON 转换为对象。
String data = /*load the json data*/
try {
JSONArray jsonArray = new JSONArray(data);
JSONObject object = jsonArray.getJSONObject(0);
JSONObject sendingReqDataSetObject = object.getJSONObject("sendingReqDataSet");
JSONArray arrayContacts = sendingReqDataSetObject.getJSONArray("contacts");
for (int i = 0; i<arrayContacts.length(); i++) {
JSONObject contactObject = arrayContacts.getJSONObject(i);
System.out.println(contactObject.getString("status"));
}
} catch (JSONException e) {
e.printStackTrace();
}