无法使用Volley将JSONArray转换为JSONObject

伊内斯·贝洛歇特(Ines Belhouchet)

我正在尝试使用Volley来使用Web服务。这是我的logcat输出:

12-14 10:45:41.750:I / Annuaire2error(4005):org.json.JSONException:值[{“ 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”:“ org.json.JSONArray类型的Derkok“,” pers_email“:” [email protected]“,” pers_phone“:” 2345512398“}]}}]无法转换为JSONObject

看来它无法访问label我的Web服务的价值如何从转换JSONArrayJSONObject

这是我的代码:

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

        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

(Android Volley)JSONObject无法转换为JSONArray

使用Python将JSONObject转换为JSONArray

将JSONObject转换为JSONArray

android json解析错误-无法将JSONArray转换为JSONObject

jsonarray无法转换为jsonobject logcat错误

JSONObject无法转换为JSONArray(Android)

JSONObject无法转换为JSONArray(Android)

JSONObject无法转换为JSONArray-Android

JSONArray类型无法转换为JSONObject

麻烦的JsonObject解析无法转换为jsonarray

错误类型 JSONArray 无法转换为 JSONObject

将JSONArray转换为Iterable <JSONObject>-Kotlin

如何将JSONArray转换为JSONObject?

如何将JSONObject转换为JSONArray

如何将 JSONObject 转换为 JSONArray?

jsonobject无法转换为jsonarray嵌套jsonarray android

“无法将org.json.simple.JSONArray强制转换为org.json.simple.JSONObject”

org.json.JSONException:无法将 org.json.JSONArray 类型的 0 值转换为 JSONObject

Android JSON解析“无法将org.json.JSONObject类型的数据转换为JSONArray”错误

org.json.JSONException:无法将 org.json.JSONArray 类型的值 [{}] 转换为 JSONObject

JSONArray java.lang.String无法转换为JSONObject

获取''org.json.JSONArray无法转换为JSONObject''

org.json.JSONObject 类型无法转换为 JSONArray

类型 org.json.JSONObject 无法转换为 JSONArray Android

org.json.JSONArray无法转换为JSONObject

类型org.json.JSONArray的值无法转换为JSONObject

错误:类型 org.json.JSONArray 无法转换为 JSONObject

org.json.JSONObject 类型的响应无法转换为 JSONArray

错误org.json.JSONObject无法转换为JSONArray