尝试解析 JSON 数据时出现此错误 W/System.err: org.json.

我正在尝试从我的休息 api 中检索客户对象。我使用 spring data jpa 生成了 api。我已经使用 volley 从 api 中检索信息。我不知道我做错了什么。因为我是 android 的新手,所以我不太清楚。有人可以帮我从我的 Json api 中解析客户对象吗?


我的 api 看起来像这样:


{

  "_embedded": {

    "customers": [

      {

        "firstName": "Alexander",

        "lastName": "arnold",

        "email": "trentarnold@liverpool.com",

        "password": "cornertakenquickly",

        "_links": {

          "self": {

            "href": "http://localhost:8080/api/customers/1"

          },

          "customer": {

            "href": "http://localhost:8080/api/customers/1"

          }

        }

      },

      {

        "firstName": "test",

        "lastName": "tester",

        "email": "dulalsujan911@gmail.com",

        "password": "12345678",

        "_links": {

          "self": {

            "href": "http://localhost:8080/api/customers/2"

          },

          "customer": {

            "href": "http://localhost:8080/api/customers/2"

          }

        }

      }

    ]

  },

  "_links": {

    "self": {

      "href": "http://localhost:8080/api/customers{?page,size,sort}",

      "templated": true

    },

    "profile": {

      "href": "http://localhost:8080/api/profile/customers"

    }

  },

  "page": {

    "size": 20,

    "totalElements": 2,

    "totalPages": 1,

    "number": 0

  }

}


慕田峪7331174
浏览 154回答 2
2回答

牛魔王的故事

做这个 :@Override&nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject json = new JSONObject(response);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject json_embedded = json.getJSONObject("_embedded");// need to access JSONObject("_embedded")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jsonArray = json_embedded.getJSONArray("customers"); // then get JSONARRAY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject customer = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; emailList.add(customer.getString("email"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; passwordList.add(customer.getString("password"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }注意:您的 json 数组(customers)在_embedded中,这就是它显示异常的原因。

阿晨1998

您需要先访问_embedded对象。try {&nbsp; &nbsp; JSONObject embedded = response.getJSONObject("_embedded");&nbsp; &nbsp; JSONArray jsonArray = embedded.getJSONArray("customers");&nbsp; &nbsp; for(int i=0; i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; JSONObject customer = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; emailList.add(customer.getString("email"));&nbsp; &nbsp; &nbsp; &nbsp; passwordList.add(customer.getString("password"));&nbsp; &nbsp; }} catch (Exception e) {&nbsp; &nbsp; e.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java