通过Intent将JSONObject传递给另一个活动

Sli*_*169 3 string android json

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

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());
}
Run Code Online (Sandbox Code Playgroud)

Zub*_*med 28

你可以轻松地通过JSONObject转换来传递你的内容,String就像在我们JSONObject使用url 发送的一些场景中一样,通过将其添加为String.所以,尝试发送它String像:

intent.putExtra("jsonObject", jsonObject.toString());
Run Code Online (Sandbox Code Playgroud)

并在另一边接收它

Intent intent = getIntent();

String jsonString = intent.getStringExtra("jsonObject");
Run Code Online (Sandbox Code Playgroud)

现在你有JSON一个String名字jsonString假设它作为一个响应,就像你从Web服务收到然后得到你的JSONObject喜欢:

JSONObject jObj = new JSONObject(jsonString);
Run Code Online (Sandbox Code Playgroud)

希望你能理解我的观点:)


nir*_*ola 0

试试这个,您还可以使用 ComplexPrefereces 类从一个活动获取对象到另一活动

在您的项目中添加 ComplexPreferences.java 类

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


/**
* This class helps to store class object in the shared preferences
*
*/

public class ComplexPreferences {

private static ComplexPreferences complexPreferences;
private Context context;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private static Gson GSON = new Gson();
Type typeOfObject = new TypeToken<Object>() {
}.getType();

private ComplexPreferences(Context context, String namePreferences, int mode) {
    this.context = context;
    if (namePreferences == null || namePreferences.equals("")) {
        namePreferences = "complex_preferences";
    }
    preferences = context.getSharedPreferences(namePreferences, mode);
    editor = preferences.edit();
}

public static ComplexPreferences getComplexPreferences(Context context,
                                                       String namePreferences, int mode) {

    if (complexPreferences == null) {
        complexPreferences = new ComplexPreferences(context,
                namePreferences, mode);
    }

    return complexPreferences;
}

public void putObject(String key, Object object) {
    if(object == null){
        throw new IllegalArgumentException("object is null");
    }

    if(key.equals("") || key == null){
        throw new IllegalArgumentException("key is empty or null");
    }

    editor.putString(key, GSON.toJson(object));
}

public void commit() {
    editor.commit();
}

public <T> T getObject(String key, Class<T> a) {

    String gson = preferences.getString(key, null);
    if (gson == null) {
        return null;
    } else {
        try{
            return GSON.fromJson(gson, a);
        } catch (Exception e) {
            throw new IllegalArgumentException("Object storaged with key " + key + " is instanceof other class");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

将对象存储在共享首选项中,如下所示:

    ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(getActivity(), "store_object", 0);
    complexPreferences.putObject("class_name", className);
    complexPreferences.commit();
Run Code Online (Sandbox Code Playgroud)

为了检索返回对象,如下所示:

    ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(DrawerActivity.this, "store_object", 0);
    ClassName className=complexPreferences.getObject("class_name", ClassName.class);
Run Code Online (Sandbox Code Playgroud)