我知道如何处理方法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());
}
答案 0 :(得分:27)
您可以轻松地通过JSONObject
转换String
,就像我们使用网址发送JSONObject
的某些情况一样,将其添加为String
。因此,请尝试将其作为String
发送,如:
intent.putExtra("jsonObject", jsonObject.toString());
并在另一方接收
Intent intent = getIntent();
String jsonString = intent.getStringExtra("jsonObject");
现在,JSON
名为String
的{{1}}假设它是一个响应,就像您从Web服务收到的那样,然后得到您的jsonString
:
JSONObject
希望你能理解我的观点:)
答案 1 :(得分: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");
}
}
}
}
将对象存储在共享首选项中,如下所示:
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(getActivity(), "store_object", 0);
complexPreferences.putObject("class_name", className);
complexPreferences.commit();
用于检索这样的后退对象:
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(DrawerActivity.this, "store_object", 0);
ClassName className=complexPreferences.getObject("class_name", ClassName.class);
答案 2 :(得分:0)
将对象作为意图传递,可串联是最好的方法。以下是从一个活动传递意图的代码
MJSONObject mObject;
Intent intent = new Intent(FromActivity.this, ToActivity.class);
Bundle userObject = new Bundle();
userObject.putSerializable("Object", mObject);
userObject.putString("method_name", "ObjectIntent");
intent.putExtras(userObject);
startActivity(intent);
在另一项活动中,使用如下方式获取意图:
String method_name = null;
MJSONObject mObject;
method_name = getIntent().getStringExtra("method_name");
if(method_name.equals("ObjectIntent"))
{
mObject= (MJSONObject) getObject.getSerializable("Object");
}
最后你的自定义对象的pojo类应该是:
public class MJSONObject implements Serializable {
private static final long serialVersionUID = 1L;
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;
}