Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing jsonarray from 1 activity to another

I am making an app in which I want o pass a json array between 2 activities .how to pass json arry from one activity to another through intents in android. can anybody help me over this?? thanks

like image 824
ekjyot Avatar asked Oct 07 '11 11:10

ekjyot


People also ask

How do I pass JSONArray in intent?

You should convert JsonArray to String then attach it to Intent and send it. JSONObject jObject = new JSONObject("Your Json Response"); Intent obj_intent = new Intent(Main. this, Main1.

Can we convert JSONArray to JSONObject?

Core Java bootcamp program with Hands on practiceWe can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

What is the difference between JSONObject and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

How is JSONArray defined?

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.


2 Answers

Intent intent = new Intent(your_activity.this, new_activity.class);
intent.putExtra("jsonArray", mJsonArray.toString());
startActivity(intent);

In the next Activity

        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();
        }
like image 64
Lalit Poptani Avatar answered Sep 29 '22 16:09

Lalit Poptani


You should convert JsonArray to String then attach it to Intent and send it.

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);

Where jObject4 is JSON Object.

Get in next Page :

Bundle b = getIntent().getExtras();
String Array=b.getString("Array");
like image 24
Venky Avatar answered Sep 29 '22 14:09

Venky