java - Intent JSONObject 到另一个 Activity

标签 java android json

我想将 JSONObject 传递到下面代码中 else 部分中的另一个 Activity ,以便我可以在其他 Activity 中使用个人资料详细信息,

public void redirectToActivity(JSONObject result, JSONObject profile){

    try {           
        String status = result.getString("status");

        if (status.equals("204")) {
            Intent intent = new Intent(this, ActivityMenu.class);
            intent.putExtra("user", "email");
            this.startActivity(intent);
        } 
        else {
            Intent intent = new Intent(this, RegisterActivity.class);
            this.startActivity(intent);
            //here I want to pass JSONObject profile to RegisterActivity
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

最佳答案

要传递JSONObject,您可以使用String。例如:

 Intent intent = new Intent(this, RegisterActivity.class);
 intent.putExtra("profile", profile.toString());
 intent.putExtra("result", result.toString());
 this.startActivity(intent);

toString() JSONObject 的实现,类 rn 将对象编码为紧凑的 JSON 字符串。在您的 RegisterActivity 中,检索字符串:

 String profile = getIntent().getStringExtra("profile");
 JSONObject profileJSON = new JSONObject(profile)

关于java - Intent JSONObject 到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801729/

相关文章:

java - 为什么这个程序总是打印1

java - 在 Java 中接收时 TCP 套接字挂起

json - AEM 将 html 信息注释插入 json

python - 我的程序有时会在 json 文件中的数据末尾写一个额外的 ] 或 }?

java - 在不跟在 '\' 之后的分隔符处分割字符串

Java Switch 在多种情况下使用变量的最佳实践

android - 解压缩文件时出现 FileNotFoundException

android - RxJava 在继续之前等待响应被检索

android - 为什么 Volley 退回到 SSLV3?

javascript - 使用 Node 在内存中处理大型 JSON 数据集