【问题标题】:Unable to fetch data from Json volley无法从 Json volley 中获取数据
【发布时间】:2017-01-23 07:38:42
【问题描述】:

我无法从这种类型的 JSON 中获取数据,我对如何从 JsonObject 内部获取数据感到困惑,我得到了“dealer_name”、“phone_no”和“address”的值,但我没有获取其他的价值。

这是我的解决方案。

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            urlLink, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d("suraj", response.toString());

            try {

                for (int i = 0; i < response.length(); i++) {

                    String name = response.getString("dealer_name");
                    String phone_no = response.getString("phone_no");
                    String add = response.getString("address");

                    JSONObject phone = (JSONObject)                       response.get(String.valueOf(i));

                    String acc_name = phone.getString("auto_dealer_id");
                    String acc_price = phone.getString("accessory_price");

                    jsonResponse = "";
                    jsonResponse += "dealer_name: " + name + "\n\n";
                    jsonResponse += "dealer_phone: " + phone_no + "\n\n";
                    jsonResponse += "dealer_add: " + add + "\n\n";

                    jsonResponse += "acc_name: " + acc_name + "\n\n";
                    jsonResponse += "acc_price: " + acc_price + "\n\n";
                }
                txt.setText(jsonResponse);
            } catch (JSONException e) {
                e.printStackTrace();         
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

    Volley.newRequestQueue(Activity3.this).add(jsonObjReq);
}

这是我的 JSON 数据:-

{ "auto_dealer_id": "1", "dealer_name": "RAJ MOTORS", "phone_no": "9004296356", "address": "THANE WEST 40002", "0": { "auto_dealer_accessory_id": "1", "auto_dealer_id": "1", "accessory_name": "CAR OILING", "accessory_price": "40" }, "1": { "auto_dealer_accessory_id": "2", "auto_dealer_id": "1", "accessory_name": "CAR WASHING", "accessory_price": "40" }, "2": { "auto_dealer_gallery_id": "1", "auto_dealer_id": "1", "image": "1.jog", "status": "1", "sort": "1", "added_date": "0000-00-00 00:00:00" } }

【问题讨论】:

  • 为了更好的数据表示,您的 0 1 2 .. json 对象应该在一个 jsonarray 中,而不是命名为 0 1 2 jsonobjects 并且对于当前问题 auto_dealer_id 等键在另一个 jsonobjects 中 0 1 2 ...跨度>

标签: android json android-volley


【解决方案1】:

试试这个代码:

 try
    {   
        String jsonString="";//your json string here
        JSONObject jObject= new JSONObject(jsonString);
        Iterator<String> keys = jObject.keys();
        while( keys.hasNext() )
        {
            String key = keys.next();
            Log.v("key Items", key);
            JSONObject innerJObject = jObject.getJSONObject(key);
            Iterator<String> innerKeys = innerJObject.keys();
            while( innerKeys.hasNext() )
            {
                String innerKkey = keys.next();
                String value = innerJObject.getString(innerKkey);
                Log.v("key = "+key, "value = "+value);
            }
        }
    }
    catch (JSONException e){   
        e.printStackTrace();    
    }

但最好将JsonObject"1","2"... 转换为JsonArray

【讨论】:

    【解决方案2】:

    既然要在 json 对象中获取 json 对象, 在这里你可以得到

    JSONObject number = response.getJSONObject("1");
    number.getString("auto_dealer_accessory_id") 
    

    等等。 但也有一些更好的方法。使用Gson 并改进来自服务器的数据结构。使用数组是更好的选择,不要忘记在尝试获取值之前检查对象或字符串是否存在。您可以使用 jsonObject.has("key")

    【讨论】:

    • Junaid hafeez 感谢您的回复。有任何选项可以使用循环获取 JsonObject。
    • 因为这些是 json 对象。如果将它们更改为数组,则可以循环它们,或者如果您知道确切的数字意味着 0、1、2、3 将保持不变,则可以循环到最后一个计数。类似 response.getJsonObject("count");
    • 要遍历对象,请参阅 rafsanahmad007 答案。他解释得很好。
    【解决方案3】:

    试试这个:

    ArrayList acc_name = new ArrayList(); ... ...

    try
        {   
          JSONObject obj=new JSONObject(responseString);
          String name = obj.getString("dealer_name");
          String phone_no = obj.getString("phone_no");
          String add = obj.getString("address");
    
            Iterator<String> keys = obj.keys();
            while( keys.hasNext() )
            {
                String key = keys.next();
    
                JSONObject innerJObject = obj.getJSONObject(key);
                Iterator<String> inKeys= innerJObject .keys();
                while( inKeys.hasNext() )
                {
                    String inKeys= keys.next();
                    acc_name.Add(innerJObject .getString(inKeys);
                    ..
                }
            }
        }
        catch (JSONException e)
        {   e.printStackTrace();    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多