Android JSON使用GSON解析数组 [英] Android JSON parsing arrays with GSON
问题描述
Adventures :
+ ------------- +
|冒险1 |
+ ------------- +
|冒险2 |
+ ------------- +
活动:
+ ------------- +
|事件1 |
+ ------------- +
|事件2 |
+ ------------- +
I'能够读出单个字符串很好,但我没有把我的头移动到数组。我收到的JSON格式是:
{
events:[
{
eid:11111111,
bid:aaaaaaaa,
bname:示例商业1,
开始:3/26/14 @ 6pm,
end:3/27/14 @ 2am,
points:50,
title:示例事件1,
description:示例事件描述,
cat:夜生活,
type:酒吧,
子类型:卡拉OK,
有效:真
},
{
eid:22222222,
bid:bbbbbbbb,
bname:示例商务2,
start:3/26/14 @ 6pm,
end:3/27/14 @ 2am,
points:
title:示例事件2,
description:示例事件描述,
cat:夜生活,
type :喜剧,
子类型:一般,
有效:真
},
{
eid:33333333,
bid:cccccccc,
bname:示例商业3,
开始:3 / 26/14 @ 6pm,
end:3/27/14 @ 2am,
points:150,
title:示例事件3 ,
description:示例事件描述,
cat:Dining,
type:Restraunt,
subtype:Chinese ,
valid:true
}
],
冒险:[
{
aid:11111111,
出价:aaaaaaaa,
开始:3/26/14 6pm,
结束:3/27/14 2am,
分 :
title:示例冒险1,
description:示例冒险描述,
cat:夜生活,
类型:酒吧,
子类型:卡拉OK,
steps_comp:2,
total_steps:5,
有效 :true
},
{
aid:2 2222222,
bid:bbbbbbbb,
start:3/26/14 6pm,
end:3/27/14 2am,
points:250,
title:示例冒险2,
description:示例冒险描述,
cat: ,
type:Bar,
subtype:Neighborhood,
steps_comp:0,
total_steps:5,
valid:true
}
]
}} p>
我使用以下(包含排球代码)解析出JSON:
RequestQueue队列= Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,full_url,null,new Response.Listener< JSONObject>(){
@Override $ b $ 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自动生成的catch块
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),blah,
Toast.LENGTH_LONG).show();
}
},新的Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// TODO自动生成的方法存根
}
}
);
queue.add(jsObjRequest);
我得到每一行都很好,但它只是一个字符串,所以我无法获取每个单独的元素。我想我可能需要在for循环中再做一次GSON调用来打破这个问题,然后放入数组中?这似乎很混乱。我已经做了一些googleing(我已经得到了这么多),但我似乎无法将这些作品放在一起,使这一个工作。任何帮助将不胜感激。
首先为每个JsonAarry创建类以容纳价值
public class Events {
private String eid;
私人字符串出价;
私人字符串bname;
私有字符串开始;
私人字符串结束;
私人弦乐分数;
私有字符串标题;
私有字符串描述;
私人字符串猫;
私有字符串类型;
私有字符串子类型;
私有布尔有效;
@Override
public String toString(){
returnEvents [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 steps_comp;
@Override
public String toString(){
returnAdventures [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;
私有布尔有效;
$ / code>
现在创建一个保存整个响应的类
public class ResponseHolder {
private ArrayList< Events>事件;
private ArrayList< Adventures>冒险;
@Override
public String toString(){
returnResponseHolder [events =+ events +,adventures =
+ adventures +];
}
public ArrayList< Events> getEvents(){
返回事件;
}
public ArrayList< Adventures> getAdventures(){
返回冒险;
}
}
最后
Gson googleJson = new Gson();
ResponseHolder rh = googleJson.fromJson(jsonArr,ResponseHolder.class);
并获取数据,例如
<$ p $ (事件e:rh.getEvents()){
System.out.println(e.toString()); p>
(Adventures e:rh.getAdventures()){
System.out.println(e.toString());
}
I've been fighting with GSON for the better part of the day. Well, not the GSON so much as my general non-understanding of lists in Android I think. Basically what is happening is I am receiving 2 arrays in JSON, which will then each be formatted and displayed in a list:
Adventures:
+-------------+
| Adventure 1 |
+-------------+
| Adventure 2 |
+-------------+
Events:
+-------------+
| Event 1 |
+-------------+
| Event 2 |
+-------------+
I'm able to read out individual string just fine, but I'm not getting my head around moving this on to arrays. JSON format I'm recieving is:
{
"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
}
]
}
I'm parsing out the JSON with the following (Volley code included):
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);
I'm getting each row just fine, but it's just a string so I'm not able to get at the individual elements for each. I'm thinking I might need to do another GSON call inside the for loop to break that one as well, then place inside of a array? This seems messy. I've done a bit of googleing(how I've gotten this far), but I can't seem to put the pieces together to make this one work. Any help would be appreciated.
First create class for each JsonAarry to hold the vales
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 + "]";
}
}
and
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;
}
Now create a class that hold the whole response
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;
}
}
and finally
Gson googleJson = new Gson();
ResponseHolder rh= googleJson.fromJson(jsonArr, ResponseHolder.class);
And get the data like
for(Events e: rh.getEvents()){
System.out.println(e.toString());
}
for(Adventures e: rh.getAdventures()){
System.out.println(e.toString());
}
这篇关于Android JSON使用GSON解析数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!