Json解析错误

时间:2016-05-10 09:16:37

标签: android json parsing

  • 我得到了回应,但是在尝试在列表中设置响应时,它会抛出错误:
  • 任何人都可以帮我这个,错误日志,课程,配对,方法如下所示

  • 我知道解析,但我不知道我需要改变什么以获得欲望输出

错误日志cat

   org.json.JSONException: Value Friend Request List at Message of type java.lang.String cannot be converted to JSONObject

 at org.json.JSON.typeMismatch(JSON.java:100)
05-09 22:47:53.542 9726-9726/com. W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:578)
05-09 22:47:53.542 9726-9726/com. W/System.err:     at com..parse.ParseContent.isSuccessWithData(ParseContent.java:101)
05-09 22:47:53.542 9726-9726/com. W/System.err:     at com..fragment.SearchFragment.onTaskCompleted(SearchFragment.java:155)

回应:

{"status":true,"Message":" List","data":[{"user_id":"4","first_name":"z","last_name":"q","email":"s","city":"c","state":"b","country":"a"},{"user_id":"17","first_name":"f","last_name":"e","email":"d","city":"c","state":"b","country":"a"},{"user_id":"19","first_name":"a","last_name":"b","email":"abc@xyz.com","city":"a","state":"b","country":"c"}]}

方法:

public ArrayList<HashMap<String, String>> getRes(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getString(KEY_SUCCESS).equals("true")) {

                arraylist = new ArrayList<HashMap<String, String>>();
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject obj = jsonArray.getJSONObject(i);
                    //  JSONObject job = obj.getJSONObject("User");

                    Log.d("obj", obj.toString());

                    String a = obj.getString(Constants.Params.a);
                    String b = obj.getString(Constants.Params.b);
                    String c = obj.getString(Constants.Params.c);
c




                    map.put(AndyConstants.Params.ID, aer_id);
                    map.put(AndyConstants.Params.FIRSTNAME, b);
                    map.put(AndyConstants.Params.LASTNAME, c);


                    Log.d("map", map.toString());
                    arraylist.add(map);

                }

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return arraylist;
    }

解析:

public boolean isSuccessWithData(String response) {
        if (TextUtils.isEmpty(response)) {
            return false;
        }
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getString(KEY_SUCCESS).equals("true")) {

                JSONObject parentObject = new JSONObject(response);
                jsonObject = parentObject.getJSONObject(KEY_MSG);

                      jsonObject=jsonObject.getJSONArray(KEY_DATA).getJSONObject(0);

                //   jsonObject = jsonObject.getJSONArray(KEY_MSG).getJSONObject(0);
                //   jsonObject = jsonObject.getJSONArray(KEY_USERINFO).getJSONObject(0);


                preferenceHelper.a(jsonObject.optString(Constants.Params.a));
                preferenceHelper.b(jsonObject.optString(Constants.Params.b));
                preferenceHelper.c(jsonObject.optString(Constants.Params.c));



                return true;
            } else {

                // AndyUtils.showErrorToast(jsonObject.getInt(KEY_MSG),activity);
                return false;

            }

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return false;
    }

1 个答案:

答案 0 :(得分:2)

错误位于jsonObject = parentObject.getJSONObject(KEY_MSG);,因为msg不是JSONObject它是String,所以请将其视为

String message = parentObject.getString(KEY_MSG);