2

我使用 gson 反序列化来自远程服务的 json 提要,我有一个提要 json 具有结构:

{
"getListContentGroupOfAppIDResult":{
"error":1,
"listContentGroup":[
{
"contentgroupID":"30",
"contentgroupImage":null,
"contentgroupName":"Series 1",
"contentgroupappID":"14"
},
{
"contentgroupID":"31",
"contentgroupImage":null,
"contentgroupName":"Series 2",
"contentgroupappID":"14"
}
],
"msg":"SUCCESS"
}

}

这是我的系列对象:

public class Series implements Parcelable {
        private String contentgroupID;
        private String contentgroupName;
        private String contentgroupImage;
        private String contentgroupappID;

        public String getcontentgroupappID() {
            return contentgroupappID;
        }

        public void setcontentgroupappID(String contentgroupappID) {
            this.contentgroupappID = contentgroupappID;
        }

        public String getcontentgroupID() {
            return contentgroupID;
        }

        public void setcontentgroupID(String contentgroupID) {
            this.contentgroupID = contentgroupID;
        }

        public String getcontentgroupName() {
            return contentgroupName;
        }

        public void setcontentgroupName(String contentgroupName) {
            this.contentgroupName = contentgroupName;
        }

        public String getcontentgroupImage() {
            return contentgroupImage;
        }

        public void setcontentgroupImage(String contentgroupImage) {
            this.contentgroupImage = contentgroupImage;
        }

        public Series() {
            super();
            // TODO Auto-generated constructor stub
        }

        public Series(String contentgroupID, String contentgroupName,
                String contentgroupImage, String contentgroupappID) {
            super();
            this.contentgroupID = contentgroupID;
            this.contentgroupName = contentgroupName;
            this.contentgroupImage = contentgroupImage;
            this.contentgroupappID = contentgroupappID;
        }

        public Series(Parcel in) {
            contentgroupID = in.readString();
            contentgroupImage = in.readString();
            contentgroupName = in.readString();
            contentgroupappID = in.readString();
        }

        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
            // TODO Auto-generated method stub
            dest.writeString(contentgroupID);
            dest.writeString(contentgroupImage);
            dest.writeString(contentgroupName);
            dest.writeString(contentgroupappID);
        }

    public static final Parcelable.Creator<Series> CREATOR = new Parcelable.Creator<Series>() {

        public Series createFromParcel(Parcel in) {
            // TODO Auto-generated method stub
            return new Series(in);
        }

        public Series[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Series[size];
        }
    };
}

这是我的 ListSeries 对象:

public class ListSeries implements Serializable {
        private ArrayList<Series> listContentGroup;// listContentGroup
        private String error;
        private String msg;

        public ArrayList<Series> getlistContentGroup() {
            return listContentGroup;
        }

        public void setlistContentGroup(ArrayList<Series> listContentGroup) {
            this.listContentGroup = listContentGroup;
        }

        public String getError() {
            return error;
        }

        public void setError(String error) {
            this.error = error;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public ListSeries() {
            // TODO Auto-generated constructor stub
        }

        public ListSeries(ArrayList<Series> listContentGroup, String error,
                String msg) {
            super();
            this.listContentGroup = listContentGroup;

            this.error = error;
            this.msg = msg;
        }
    }

现在正在使用 gson 将 json 反序列化为对象

 // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            static InputStream is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    Gson gson = new Gson();
            try {
                   ListSeries list = gson.fromJson(new InputStreamReader(is),
                        ListSeries.class);
                Log.e("MSG", list.getMsg());

但不能反序列化到对象。请帮我!

4

0 回答 0