如何将jsonarray转换为json对象

时间:2016-10-05 06:39:07

标签: android json android-json

我有以下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();
        }

    }

4 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取对象:

jsonArray = jsonObject.getJSONArray("contacts");

for(int i=0; i<jsonArray.size(); i++) {
    JSONObject jobject = jsonArray.getJSONObject(i)
}

答案 1 :(得分:0)

JSOnArray array=new JSONArray(json_string);

JSONObject obj=array.getObjetc("sendingReqDataSet");

jsonArray=obj.getJSONArray("contacts");

答案 2 :(得分:0)

使用Gson库(https://github.com/google/gson

图书馆支持转换为JSON或从JSON转换为对象。

答案 3 :(得分:0)

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();
        }