gpt4 book ai didi

java - 如何在android中使用volley获取Json对象中的Json数组并在listview代码和Xml Avilavble中设置此json数据

转载 作者:行者123 更新时间:2023-12-02 01:26:37 26 4
gpt4 key购买 nike

我正在使用 volley 进行 JSON 解析。我想使用 GET 从服务器端获取一些我尝试发送的数据。谁能告诉我怎么做?以下是我的代码。我还尝试了 HashMapJSONobject 但收到此错误。

of type org.json.JSONObject cannot be converted to JSONArray

还有更多,请告诉我原因。

JSON

{
"Baby Care": [{
"id": "35",
"sub_category": "Baby Food"
}, {
"id": "36",
"sub_category": "Diapers & Wipes"
}, {
"id": "38",
"sub_category": "Baby Accessories"
}],
"Personal Care": [{
"id": "27",
"sub_category": "Skin Care"
}, {
"id": "28",
"sub_category": "Hair Care"
}, {
"id": "29",
"sub_category": "Oral Care"
}, {
"id": "30",
"sub_category": "Bath, Face & Hand Wash"
}],
"Grocery": [{
"id": "51",
"sub_category": "Cheese"
}, {
"id": "52",
"sub_category": "Cakes & Toast"
}, {
"id": "63",
"sub_category": "Beverages"
}, {
"id": "69",
"sub_category": "Dal & Pulses"
}, {
"id": "70",
"sub_category": "Rice"
}],
"Household": [{
"id": "59",
"sub_category": "Kitchen Appliances"
}, {
"id": "60",
"sub_category": "Vacuum and Floor care"
}],
"Women": [{
"id": "64",
"sub_category": "Jewellery"
}, {
"id": "65",
"sub_category": "Make-up"
}, {
"id": "66",
"sub_category": "Perfume"
}],
"Men": [{
"id": "67",
"sub_category": "Perfume"
}, {
"id": "68",
"sub_category": "Accessories"
}]
}

如何使用 Volley 获取此 JSON 格式并设置为 ListView 我尝试了下面的代码,但没有得到任何对象:

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(productUrl, new Response.Listener < JSONArray > () {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {

JSONArray ja = response.getJSONArray(i);
for (int j = 0; j < ja.length(); j++) {
JSONObject jb = ja.getJSONObject(j);
SubProduct movie = new SubProduct();
movie.setId(jb.getInt("id"));
movie.setname(jb.getString("sub_category"));

movieList.add(movie);
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();

} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);

最佳答案

使用:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
productUrl, null,
new Response.Listener<JSONObject>() {

而不是:

   JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(productUrl, new Response.Listener<JSONArray>(){

使用下面的代码获取数组的动态键:

Iterator<?> keys = response.keys();
while( keys.hasNext() ) {
String key = (String)keys.next();
if ( response.get(key) instanceof JSONObject ) {
System.out.println(key); // do whatever you want with it
}
}

您可以使用以下代码获取对象:

JSONArray arr = response.getJSONArray(key);
JSONObject element;
for(int i = 0; i < arr.length(); i++){
element = arr.getJSONObject(i);
}

关于java - 如何在android中使用volley获取Json对象中的Json数组并在listview代码和Xml Avilavble中设置此json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56804366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com