使用GSON解析Android JSON数组
一天中的大部分时间我都在和格森打架。好吧,我想不是GSON,而是我对Android中列表的一般不理解。基本上,我正在接收2个JSON数组,然后每个数组都将被格式化并显示在一个列表中:使用GSON解析Android JSON数组,android,json,gson,Android,Json,Gson,一天中的大部分时间我都在和格森打架。好吧,我想不是GSON,而是我对Android中列表的一般不理解。基本上,我正在接收2个JSON数组,然后每个数组都将被格式化并显示在一个列表中: Adventures: +-------------+ | Adventure 1 | +-------------+ | Adventure 2 | +-------------+ Events: +-------------+ | Event 1 | +-------------+ | Event
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(包括截取代码):
RequestQueue queue=Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.Method.GET,full_url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
字符串示例=response.toString();
Log.e(“JSON响应”,示例);
试一试{
JsonParser JsonParser=新的JsonParser();
JsonObject jo=(JsonObject)jsonParser.parse(示例);
JsonArray jsonArr=jo.getAsJsonArray(“事件”);
//杰索纳尔。
Gson googleJson=new Gson();
ArrayList jsonObjList=googleJson.fromJson(jsonArr,ArrayList.class);
System.out.println(“列表大小为:+jsonObjList.size());
System.out.println(“列表元素是:“+jsonObjList.toString()”);
对于(int i=0;i
我得到的每一行都很好,但它只是一个字符串,所以我无法得到每一行的单独元素。我在想,我可能需要在for循环中执行另一个GSON调用来打破这个循环,然后将其放入数组中?这看起来很乱。我已经做了一些谷歌搜索(我是如何做到这一点的),但我似乎无法将各个部分组合起来,使这一个工作。任何帮助都将不胜感激。我无法为您提供帮助,但我认为您正在努力从jsonarray获取每个值,如果您在for循环中使用此代码的话
jsonobject = jsonArr.getJSONObject(i);
if (jsonobject != null)
{
String ID= jsonobject.getString("eid");
String BID= jsonobject.getString("bid");
// Like this implement for other jsonobjects
}
我无法理解您,但我认为您正在努力从jsonarray获取每个值,如果您在for循环中使用了这段代码的话
jsonobject = jsonArr.getJSONObject(i);
if (jsonobject != null)
{
String ID= jsonobject.getString("eid");
String BID= jsonobject.getString("bid");
// Like this implement for other jsonobjects
}
为每个json对象创建Java对象。例如:
public class Event {
private String bid;
private int eid;
}
public class Adventure {
private String bid;
private int eid;
// rest of the fields
}
然后,创建一个包含以下对象的类:
public class JsonResponse {
private ArrayList<Event> events;
private ArrayList<Adventure> adventures;
}
为每个json对象创建Java对象。例如:
public class Event {
private String bid;
private int eid;
}
public class Adventure {
private String bid;
private int eid;
// rest of the fields
}
然后,创建一个包含以下对象的类:
public class JsonResponse {
private ArrayList<Event> events;
private ArrayList<Adventure> adventures;
}
首先为每个JSONARRY创建类来保存值
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 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;
}
}
然后像这样获取数据
for(Events e: rh.getEvents()){
System.out.println(e.toString());
}
for(Adventures e: rh.getAdventures()){
System.out.println(e.toString());
}
首先为每个JSONARRY创建类来保存值
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 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;
}
}
然后像这样获取数据
for(Events e: rh.getEvents()){
System.out.println(e.toString());
}
for(Adventures e: rh.getAdventures()){
System.out.println(e.toString());
}
我也有同样的问题。我有这样的json。
{
“最新发布”:[
{
“发布id”:“7”,
“张贴标题”:“戒指”,
“photo_img”:“images/posting/img_12121212.png.png”,
“正常价格”:空
},
{
“发布id”:“8”,
“发布标题”:“dsadsd”,
“photo_img”:“images/posting/img_1212121216.jpg”,
“正常价格”:空
},
{
“发布id”:“9”,
“发布标题”:“dasd”,
“photo_img”:“images/posting/img_121212122.png”,
“正常价格”:空
}
],
“促销活动”:[
{
“发布id”:“10”,
“发布标题”:“dasd”,
“photo_img”:“images/posting/img_121212123.png”,
“正常价格”:空
}
]
}
你说的
JsonResponse-jsonObjList=googleJson.fromJson(jsonArr,JsonResponse.class);
但我们在数据集类中有两个数组,我们将其传递给“jsonArr”进行解析。
在日志中获取数组的每个单独响应。
我的数据集类
公共类BannerAdvertismentVO实现可序列化{
私人ArrayList推广上市;
私人ArrayList最新发布;
public ArrayList getPromotion_listing(){
退换货上市;
}
公共无效集合促销列表(ArrayList促销列表){
this.promotion\u listing=promotion\u listing;
}
public ArrayList getLatest_posting(){
返回最新发布的邮件;
}
公共作废设置最新发布(ArrayList最新发布){
this.latest_posting=最新_posting;
}
}
私有字符串过帐\u id;
私人字符串过帐(标题);;
私人字符串照片;
私人字符串正常价格;
公共字符串getPosting_id(){
返回过帐标识;
}
公共无效setPosting\u id(字符串posting\u id){
this.posting\u id=posting\u id;
}
公共字符串g