我正在尝试将 Json 对象放入我的移动应用程序中,并解析它以将其添加到地图中

I am trying to get Json Object into my mobile app, and parse it to be added in Map

这是 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"
    }
]
}

我想获得 Shops[0],Shops[1]..... 从而得到 shop_id,lat,long...

我正在使用 Volley 库。

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);
}

`

将项目的类型更改为列表,如下所示:

    ArrayAdapter<String> adapter;
    ArrayList<JSONObject> items;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView=(ListView)findViewById(R.id.listv);
        items=new ArrayList<JSONObject>();
        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);
                } 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);
}

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

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

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");
}