通过意向传递的JSONObject到另一个活动 [英] Passing JSONObject to another activity via Intent

查看:1741
本文介绍了通过意向传递的JSONObject到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何处理方法jsonObj.toString()做的,但我有一个理由不能这样做。我的JSONObject有一个JSONArray里面有太多的数据,所以如果我将其转换为字符串,然后转换回来,我害怕它到达字符串类型的限制。
所以,我想通过一个纯粹的对象,而不是使用该方法。我实现Parcelable接口,这样做,但我得到了错误了java.lang.RuntimeException:包裹:不能编组值

 公共类MJSONObject实现Parcelable {
私人的JSONObject的JSONObject;公共MJSONObject(){
}公共MJSONObject(的JSONObject的JSONObject){
    this.jsonObject =的JSONObject;
}公共无效集(的JSONObject的JSONObject){
    this.jsonObject =的JSONObject;
}公众的JSONObject得到(){
    返回的JSONObject;
}@覆盖
公众诠释describeContents(){
    // TODO自动生成方法存根
    返回0;
}公共静态最终Parcelable.Creator< MJSONObject> CREATOR =新Parcelable.Creator< MJSONObject>(){
    公共MJSONObject createFromParcel(包裹中){
        返回新MJSONObject(中);
    }    公共MJSONObject [] newArray(INT大小){
        返回新MJSONObject【尺寸】;
    }
};@覆盖
公共无效writeToParcel(DEST包裹,INT标志){
    dest.writeValue(JSONObject的);
}私人MJSONObject(包裹中){
    JSONObject的=(的JSONObject)in.readValue(JSONObject.class.getClassLoader());
}


解决方案

您可以很容易地通过字符串的JSONObject C $ C>像在某些情况下,我们通过追加它作为一个字符串发送的JSONObject 与网址。因此,尝试把它发送为字符串这样的:

  intent.putExtra(JSONObject的jsonObject.toString());

和接收它的另一面。

 意向意图= getIntent();串jsonString = intent.getStringExtra(JSONObject的);

现在你有你的 JSON 字符串名为 jsonString 假设它当从Web服务接收到你喜欢的响应,然后让你的的JSONObject 这样的:

 的JSONObject jObj =新的JSONObject(jsonString);

希望你能明白我的观点是:)

I know what to do with the method jsonObj.toString() but I have a reason to can not do so. My JSONObject has a JSONArray inside that has too much data, so If I convert it to String then transform back that I afraid of it reaches the limit of String type. So I want to pass a pure object rather than use that method. I implement Parcelable interface to do that but I got the error "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());
}

解决方案

You can easily pass your JSONObject by converting it in String like in some scenarios we send JSONObject with url by appending it as a String. So, Try to send it as String like:

intent.putExtra("jsonObject", jsonObject.toString());

And receive it on other side

Intent intent = getIntent();

String jsonString = intent.getStringExtra("jsonObject");

Now you have your JSON in a String named jsonString assume it as a response like when you received from Web Service and then get your JSONObject like:

JSONObject jObj = new JSONObject(jsonString);

hope you will understand what my point is :)

这篇关于通过意向传递的JSONObject到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆