如何使用Volley解析JSON? [英] How to parse JSON using Volley?

查看:144
本文介绍了如何使用Volley解析JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像这样的json输出

i have some json output like this

{
    "message": "success",
    "battery": "AHAJAJ1DH13T0021",
    "data": {
        "id": 6,
        "userId": 3,
        "shopId": 1,
        "transactionStatus": "PENDING",
        "expiredAt": "2019-01-04T03:01:18.878Z",
        "updatedAt": "2019-01-04T02:01:18.916Z",
        "createdAt": "2019-01-04T02:01:18.916Z",
        "paymentId": null,
        "batteryNo": null
    },
    "shopData": {
        "id": 1,
        "name": "test1",
        "tel": "555",
        "address": "cikarang",
        "description": "showroom",
        "latitude": "-6.307923199999999",
        "longitude": "107.17208499999992",
        "open_time": "10.00",
        "battery_available": 16,
        "battery_booked": 1,
        "status": 1,
        "createdAt": "2018-12-28T03:59:55.156Z",
        "updatedAt": "2019-01-04T02:01:18.940Z"
    }
}

我像这样用凌空抽空实现

and i implement using volley like this

StringRequest request = new StringRequest(Request.Method.POST, ApiService.ORDER_BATTERY, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try{
                            BookBattery bookBattery = new BookBattery();
                            JSONObject jsonObject = new JSONObject(response);
                        if (!jsonObject.has("success")) {
                            JSONObject object = jsonObject.getJSONObject("battery");
                            String data =  object.getString("");
                            JSONArray jsonArray = jsonObject.getJSONArray("data");




                        } else {
                            Log.e("Your Array Response", "Data Null");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("error is ", "" + error);
                    }
                }) {

                    //This is for Headers If You Needed
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Content-Type", "application/json; charset=UTF-8");
                        params.put("token", TokenUser);
                        return params;
                    }

                    //Pass Your Parameters here
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("shopId", String.valueOf(shopId));
                        //params.put("Pass", PassWord);
                        return params;
                    }
                };

                AppController.getInstance().addToRequestQueue(request, tag_json_obj);

但无法正常工作,请非常感谢

but not working, please help thanks a lot

推荐答案

使用 http://jsonviewer.stack.hu / [查看json数据结构]

Use http://jsonviewer.stack.hu/ [To see json data structure]

  • 括号 {} 是一个JSONObject
  • 括号 [] 是一个JSONArray

  • braces {} its an JSONObject
  • brackets [] its an JSONArray

 public void parseJson() {
 try {
 JSONObject jsonObject = new JSONObject(jsonParsing);
 boolean isSuccess = jsonObject.getString("message").contains("success");
 if (isSuccess) {

 JSONObject jsonObjectData = jsonObject.getJSONObject("data");
 String userId = jsonObject.getString("userId");

 JSONObject jsonObjectShopData = jsonObject.getJSONObject("shopData");
 String name = jsonObject.getString("tel");

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

这篇关于如何使用Volley解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆