解析 JSON URL - JSON 异常错误

Parse JSON URL - JSONException Error

请你帮帮我,我正在尝试从 URL 解析 JSON 文件。下面是我用来抓取文件并最初只是将其发布到 TextView 但现在我想解析标签并使用它们的代码?

private void postData(final String param, final TextView tv) {

    final RequestQueue request = Volley.newRequestQueue(this);

    JSONObject postReq = new JSONObject(Request.Method.GET, url_login, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("Error [" + error + "]");

        }
    }) {

        @Override
        public Map getHeaders() throws AuthFailureError {
            Map headers = new HashMap();
            headers.put("Accept", "application/json");
            System.out.println(headers);
            return headers;
        }
    };
    request.add(postReq);
}

下面是 JSON 的示例

{
“UserInfo”: {
    “User”: {
        “name”: “Craig”,
        “surname”: "Churchill",
        "userId": "1463353",
        "userAlias": "Craig"
    }
  }
}

我试过this,但总是失败,就是做不好。

{
    "UserInfo": {
        "User": {
            "name": "Craig",
            "surname": "Churchill",
            "userId": "1463353",
            "userAlias": "Craig"
        }
    }
}

这是一个有效的 json。我在 http://jsonlint.com/ 上试过了,但是你有一些双逗号的编码很奇怪。我把 json 放在上面,用右双逗号结尾,因为 jsonlint 在验证 json 之前给出错误,直到我更改它们。

问题可能在于您如何访问该对象及其子对象。试试这个方法。

@Override
public void onResponse(JSONObject response) {
    try {
        JSONObject userObj = response.getJSONObject("UserInfo")
                            .getJSONObject("User");
        String name = userObj.getString("name");
        ...
    } catch (JSONException jsEx) {
        jsEx.printStackTrace();
    }

}