我正在尝试将Json Object放入我的移动应用程序中,然后解析它以添加到Map中

时间:2015-11-01 12:30:44

标签: android json android-volley

这是Json对象

{
"Shops": [
    {
        "shop_id": "916TCR",
        "lat": "10.512573",
        "long": "76.255868",
        "Address": "******"
    },
    {
        "shop_id": "RKTCR",
        "lat": "10.527642",
        "long": "76.214435",
        "Address": "Sanfrncisco,USA"
    },
    {
        "shop_id": "LSTCR",
        "lat": "10.527642",
        "long": "76.214435",
        "Address": "afgfagra"
    },
    {
        "shop_id": "WBSTCR",
        "lat": "10.527642",
        "long": "76.214435",
        "Address": "agkangj"
    },
    {
        "shop_id": "BHTTCR",
        "lat": "10.226967",
        "long": "76.193833",
        "Address": "gjognje"
    },
    {
        "shop_id": "KFCTCR",
        "lat": "10.527642",
        "long": "76.214435",
        "Address": "aijaogv"
    },
    {
        "shop_id": "MCTCR",
        "lat": "10.505201",
        "long": "76.269635",
        "Address": "plmqntonf"
    },
    {
        "shop_id": "BHBTCR",
        "lat": "10.527642",
        "long": "76.214435",
        "Address": "agkbajgoj"
    },
    {
        "shop_id": "DMSTCR",
        "lat": "10.528698",
        "long": "76.201991",
        "Address": "fajbjab"
    },
    {
        "shop_id": "CKGTCR",
        "lat": "10.268945",
        "long": "76.157043",
        "Address": "ajnrgj"
    }
]
}

我想成为商店[0],商店[1] ..... 从而获得shop_id,lat,long ......

我正在使用Volley Library。

Java代码

ArrayAdapter<String> adapter;
ArrayList<String> items;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView=(ListView)findViewById(R.id.listv);
    items=new ArrayList<String>();
    adapter=new ArrayAdapter(this, R.layout.item_layout,R.id.txt,items);
    listView.setAdapter(adapter);
}
  String url ="Returns Json file"
  JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url,new Response.Listener<JSONArray>() {
         public void onResponse(JSONArray jsonArray){
          for (int i=0;i<jsonArray.length();i++) {
try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    items.add(jsonObject.getString("shop_id"));
                    items.add(jsonObject.getString("Address"));
                    items.add(jsonObject.getString("lat"));
                    items.add(jsonObject.getString("long"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                    adapter.notifyDataSetChanged();
                }
            }
        },new Response.ErrorListener() {
        @Override
    public void onErrorResponse(VolleyError volleyError){
            Log.e("Error", "Unable to parse json array");
        }
    });

    requestQueue.add(jsonArrayRequest);
}

`

2 个答案:

答案 0 :(得分:0)

将项目类型更改为List,如下所示:

#include <iostream>

class test
{
private:
    template<class T, class T2>
    using both_int = std::enable_if_t<std::is_integral<T>::value && std::is_integral<T2>::value>;
private:
    int _a, _b;
public:

    template<class T, class T2 = T, class = both_int<T, T2>>
    constexpr test(const T &a, const T2 &b = 1)
        : _a(a), _b(b)
    {
        static_assert(b != 0, "Division by zero!");
    }
};

int main()
{
    test obj(1, 0); //ERROR

    test obj2(0, 1); //OK

    std::cin.get();
}

你可以获得item.get(index)来获取第n个JSONObject并通过item.get(index).getString(attr name)获取它的attr。 我建议你使用Gson并定义一个模型类,它会更好。

答案 1 :(得分:0)

你得到的是一个json对象,其中包含一个名为&#34; Shops&#34;的数组。要获取Shops数组中的对象,首先需要将响应转换为JSONObject(在本例中我称之为responseObject),然后获取其Shops数组(shopsArray),然后遍历它以获取每个对象(shopObject)和他们的名字/值:

JSONArray shopsArray = responseObject.getJSONArray("Shops");

for (int i = 0, i < shopsArray.length(); i++) {
    JSONObject shopObject = shopsArray.getJSONObject(i);
    String shopId = shopObject.getString("shop_id");
    String latitude = shopObject.getString("lat");
    String longitude = shopObject.getString("long");
    String address = shopObject.getString("Address");
}