0

我是 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对不起我的英语不好

4

1 回答 1

0

实现 ParseAble 类并将带有对象的意图发送到另一个活动

    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");
于 2012-09-12T08:00:31.673 回答