gpt4 book ai didi

使用 GSON 的 Android JSON 解析数组

转载 作者:行者123 更新时间:2023-11-29 14:49:39 25 4
gpt4 key购买 nike

一天中的大部分时间我都在与 GSON 作斗争。好吧,与其说是 GSON,不如说是我对 Android 列表的一般不了解。基本上发生的事情是我收到 2 个 JSON 数组,然后每个数组都将被格式化并显示在列表中:

Adventures:
+-------------+
| Adventure 1 |
+-------------+
| Adventure 2 |
+-------------+
Events:
+-------------+
| Event 1 |
+-------------+
| Event 2 |
+-------------+

我能够很好地读出单个字符串,但我没有考虑将其移至数组。我收到的 JSON 格式是:

{
"events": [
{
"eid": "11111111",
"bid": "aaaaaaaa",
"bname": "Example Business 1",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 1",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"valid": true
},
{
"eid": "22222222",
"bid": "bbbbbbbb",
"bname": "Example Business 2",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 2",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Comedy",
"subtype": "General",
"valid": true
},
{
"eid": "33333333",
"bid": "cccccccc",
"bname": "Example Business 3",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "150",
"title": "Example Event 3",
"description": "Example Event Description",
"cat": "Dining",
"type": "Restraunt",
"subtype": "Chinese",
"valid": true
}
],
"adventures": [
{
"aid": "11111111",
"bid": "aaaaaaaa",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "150",
"title": "Example Adventure 1",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"steps_comp": "2",
"total_steps": "5",
"valid": true
},
{
"aid": "22222222",
"bid": "bbbbbbbb",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "250",
"title": "Example Adventure 2",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Neighborhood",
"steps_comp": "0",
"total_steps": "5",
"valid": true
}
]

我正在使用以下内容解析 JSON(包括 Volley 代码):

RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, full_url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String example = response.toString();
Log.e("JSON Response", example);

try {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(example);
JsonArray jsonArr = jo.getAsJsonArray("events");
//jsonArr.
Gson googleJson = new Gson();
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);

System.out.println("List size is : "+jsonObjList.size());
System.out.println("List Elements are : "+jsonObjList.toString());

for (int i = 0; i < jsonObjList.size(); i++){
String item = jsonObjList.get(i).toString();
System.out.println("Item " + i + " : " + item);
}

Toast.makeText(getApplicationContext(), "0: "+jsonObjList.get(0),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "blah",
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}
);
queue.add(jsObjRequest);

我得到的每一行都很好,但它只是一个字符串,所以我无法获取每一行的各个元素。我在想我可能需要在 for 循环中进行另一个 GSON 调用来打破那个循环,然后放置在数组中?这看起来很乱。我已经做了一些 googleing(我是如何走到这一步的),但我似乎无法将各个部分放在一起来完成这个工作。任何帮助将不胜感激。

最佳答案

首先为每个 JsonAarry 创建类来保存值

public class Events {
private String eid;
private String bid;
private String bname;
private String start;
private String end;
private String points;
private String title;
private String description;
private String cat;
private String type;
private String subtype;
private boolean valid;
@Override
public String toString() {
return "Events [eid=" + eid + ", bid=" + bid + ", bname=" + bname
+ ", start=" + start + ", end=" + end + ", points=" + points
+ ", title=" + title + ", description=" + description
+ ", cat=" + cat + ", type=" + type + ", subtype=" + subtype
+ ", valid=" + valid + "]";
}

}

public class Adventures {
private String aid;
private String bid;
private String start;
private String end;
private String points;
private String title;
private String description;
private String cat;
private String type;
private String subtype;
private String steps_comp;
@Override
public String toString() {
return "Adventures [aid=" + aid + ", bid=" + bid + ", start=" + start
+ ", end=" + end + ", points=" + points + ", title=" + title
+ ", description=" + description + ", cat=" + cat + ", type="
+ type + ", subtype=" + subtype + ", steps_comp=" + steps_comp
+ ", total_steps=" + total_steps + ", valid=" + valid + "]";
}
private String total_steps;
private boolean valid;

}

现在创建一个包含整个响应的类

public class ResponseHolder {
private ArrayList<Events> events;
private ArrayList<Adventures> adventures;
@Override
public String toString() {
return "ResponseHolder [events=" + events + ", adventures="
+ adventures + "]";
}
public ArrayList<Events> getEvents() {
return events;
}
public ArrayList<Adventures> getAdventures() {
return adventures;
}
}

最后

Gson googleJson = new Gson();
ResponseHolder rh= googleJson.fromJson(jsonArr, ResponseHolder.class);

并获取类似的数据

        for(Events e: rh.getEvents()){
System.out.println(e.toString());
}

for(Adventures e: rh.getAdventures()){
System.out.println(e.toString());
}

关于使用 GSON 的 Android JSON 解析数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685948/

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