我知道如何使用jsonObj.toString()方法,但是由于我的JSONObject中包含一个数据量过大的JSONArray,因此如果将其转换为String类型再进行转换,我担心会达到String类型的限制。因此,我想传递一个纯粹的对象而不是使用该方法。我实现了Parcelable接口来实现这个目标,但是我遇到了错误"java.lang.RuntimeException: Parcel: unable to marshal value"。
public class MJSONObject implements Parcelable {
private JSONObject jsonObject;
public MJSONObject() {
}
public MJSONObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public void set(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public JSONObject get() {
return jsonObject;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public static final Parcelable.Creator<MJSONObject> CREATOR = new Parcelable.Creator<MJSONObject>() {
public MJSONObject createFromParcel(Parcel in) {
return new MJSONObject(in);
}
public MJSONObject[] newArray(int size) {
return new MJSONObject[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(jsonObject);
}
private MJSONObject(Parcel in) {
jsonObject = (JSONObject) in.readValue(JSONObject.class.getClassLoader());
}
intent.putExtra("jsonObject", (Serializable) jsonObject);
- Rakeeb Rajbhandari