【问题标题】:passing jsonarray from 1 activity to another将 jsonarray 从 1 个活动传递到另一个活动
【发布时间】:2011-10-07 11:20:11
【问题描述】:

我正在制作一个应用程序,我想在 2 个活动之间传递一个 json 数组。如何通过 android 中的意图将 json 数组从一个活动传递到另一个活动。有人可以帮我解决这个问题吗? 谢谢

【问题讨论】:

    标签: android android-intent arrays


    【解决方案1】:
    Intent intent = new Intent(your_activity.this, new_activity.class);
    intent.putExtra("jsonArray", mJsonArray.toString());
    startActivity(intent);
    

    在下一个活动中

            Intent intent = getIntent();
            String jsonArray = intent.getStringExtra("jsonArray");
    
            try {
                JSONArray array = new JSONArray(jsonArray);
                System.out.println(array.toString(2));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    【讨论】:

    • intent.putExtra("jsonArray", mJsonArray.toString());这里的 mJsonArray 是什么?
    • mJsonArray 是 JSONArray 的对象
    【解决方案2】:

    您应该将 JsonArray 转换为 String,然后将其附加到 Intent 并发送。

    JSONObject jObject = new JSONObject("Your Json Response");
    Intent obj_intent = new Intent(Main.this, Main1.class);
    Bundle b = new Bundle();                
    b.putString("Array",jObject4.toString());
    obj_intent.putExtras(b);
    

    其中 jObject4 是 JSON 对象。

    进入下一页:

    Bundle b = getIntent().getExtras();
    String Array=b.getString("Array");
    

    【讨论】:

    • 它只是传递 JSONObject,而不是 JSONArray。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2017-12-10
    • 2012-07-05
    • 2015-01-14
    • 2018-05-31
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多