我是Android开发的新手,它必须是一个简单的问题,但我无法弄清楚。
我的应用程序得到一个形式的json:
[
{
'src':1,
'title':'The Black Eyed Peas - Lets Get It Started',
'id':1,
'slots':[0,10],
'prev':[0,1,2,3]
},
{
'src':2,
'title':'Carly Ray Jepsen - Call Me Maybe',
'id':2,
'slots':[0,10],
'prev':[0,1,2,3]
},
{
'src':3,
'title':'Kris Kross - Jump',
'id':3,
'slots':[0,10],
'prev':[0,1,2,3]
}, .... //several identical
]
然后我解析它。
for(int i = 0; i<json.length(); i++)
{
JSONObject jo = (JSONObject) json.get(i);
String src = jo.getString("src");
String title = jo.getString("title");
String id = jo.getString("id");
//What should do next?
}
我需要创建一个新的数据类型来使用。我该怎么做? PS抱歉我的英文不好
答案 0 :(得分:0)
实现ParseAble类并使用object将intent发送到另一个活动
public class JSONDATA implements Parcelable {
private String src;
private String title;
private String id;
// Collect from json array
public JSONDATA(JSONObject jo) {
try {
String src = jo.getString("src");
String title = jo.getString("title");
String id = jo.getString("id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Parcelable.Creator<JSONDATA> getCreator() {
return CREATOR;
}
//read from Intent;
private JSONDATA(Parcel in) {
src = in.readString();
title = in.readString();
id = in.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(src);
dest.writeString(title);
dest.writeString(id);
}
public static final Parcelable.Creator<JSONDATA> CREATOR = new Parcelable.Creator<JSONDATA>() {
public JSONDATA createFromParcel(Parcel in) {
return new JSONDATA(in);
}
public JSONDATA[] newArray(int size) {
return null;
}
};
}
然后
Intent intent = new Intent();
JSONObject jo = (JSONObject) json.get(i);
JSONDATA data = new JSONDATA(jo);
intent.putExtra("DATA", data);
sendBroadCastIntent(i,"YOUR_ACTIVITY");