我试图使用截击来消费一个web服务。这是我的logcat输出:
12-14 10:45:41.750:i/annuaire2错误(4005):org.json.jsonexception:value[{“label”:[{“pers-nom”:“yen”,“pers-id”:“1”,“pers-prenom”:“ines”,“pers-email”:“[email protected]”,“pers-phone”:“443456784”,{“pers-nom”:“jamin”,“pers-id”:“2”,“pers-prenom”:“derkok”,“pers-email”:“[email protected]”,org.json.jsonarray类型的“pers-phone”:“2345512398”}]}]无法转换为jsonobject
它似乎无法访问我的web服务的label值。如何从JSONArray转换为JSONObject
这是我的代码:

mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.i(TAG,response.toString());
        try{
            JSONObject value = response.getJSONObject("label");
            String essai = value.getString("pers_nom");
            /**
             * just for check
             * */
            Toast.makeText(getActivity(), ""+essai, Toast.LENGTH_SHORT).show();
            Log.d("volley output", essai);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
},new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getActivity(), "errrrroooor", Toast.LENGTH_LONG).show();
        Log.i(TAG+"error",error.getMessage());
    }
});

mRequestQueue.add(jr);

最佳答案

得到的响应是jsonarray而不是jsonobject

public void onResponse(JSONObject response)// resposne should be JSONArray
// Change it to JSONArray

这个
JSONObject value = response.getJSONObject("label"); // wrong
// label is a json array

你的JSON
[ // json array node
    { // json object node
        "label": [ // json array label
            {
                "pers_nom": "yen",
                "pers_id": "1",
                "pers_prenom": "ines",
                "pers_email": "[email protected]",
                "pers_phone": "443456784"
            },
            {
                "pers_nom": "Jamin",
                "pers_id": "2",
                "pers_prenom": " Derkok",
                "pers_email": "[email protected]",
                "pers_phone": "2345512398"
            }
        ]
    }
]

应该有点像
 @Override
 public void onResponse(JSONArray response) {
 Log.i(TAG,response.toString());
 try{
  JSONObject value = response.getJSONObject(0);
  JSONArray jr = value.getJSONArray("label");
  for(int i=0;i<jr.length();i++)
  {
  JSONObject jb = (JSONObject) jr.get(i);
  String pers_nom = jb.getString("pers_nom");
  Log.i(".........",pers_nom);
  ...// similarly for id and so on..
  }
  ...// rest of the code

编辑:
 Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>()
        {

            @Override
            public void onResponse(JSONArray response) {
                // TODO Auto-generated method stub

            }

        };
        Response.ErrorListener errorListener = new Response.ErrorListener()
        {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }

        };

使用
    JsonArrayRequest  jr =new JsonArrayRequest(Request.Method.GET,url, listener, errorListener) ;

编辑2:
我查了一下来源,如下所示
 public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
            super(Method.GET, url, null, listener, errorListener);

        }

09-25 18:55