【问题标题】:How to access the json objects using volley如何使用 volley 访问 json 对象
【发布时间】:2018-10-02 19:48:40
【问题描述】:

这是我访问 json 对象并在文本视图中设置 uuid 的 textview 但没有任何反应的 api 响应

请提供从 api 响应访问 json 对象的代码

{
 "success":true,
 "data {
   "serial_key_id":"75",
   "order_id":"0",
   "product_id":"0",
   "serial_key":"WURYFO",
   "valid_till":null,
   "limit":"0",
   "uuid":"",
   "used":false
   }
}

私有 void jsonobject() {

    String url = "http://mylocalpay.com/?serial_key_api=1&coupon=WURYFO";
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.i("msg", "response" + response);
                    try {
                        JSONObject success = response.getJSONObject("success");
                        JSONObject data = response.getJSONObject("data");
                        String serial_key_id = data.getString("serial_key_id");
                        String order_id = data.getString("order_id");
                        String product_id = data.getString("product_id");
                        String serial_key = data.getString("serial_key");
                        String limit = data.getString("limit");
                        String uuid = data.getString("uuid");
                        boolean used = data.getBoolean("used");
                        JSONObject valid_till = data.getJSONObject("valid_till");
                        textView.setText(uuid);
                        System.out.println(serial_key);


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

            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error

                }

            });
}

}

【问题讨论】:

  • 到目前为止您尝试过什么?请发布您的具体问题。
  • 为此使用 GSON。!

标签: java android json android-recyclerview gson


【解决方案1】:

您需要将此 JSON 解析为 java 对象。您可以编写自己的代码来执行此操作(这是一项非常艰巨的任务),也可以使用 Google 的 GSON 库。

GSON GitHub page

你可以这样使用这个库

Gson gson = new Gson();
String jsonInString = "{'serial_key_id' : '75'}";
YourClass yourClass = gson.fromJson(jsonInString, YourClass.class);

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多