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
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.
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.
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.
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.
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();
}
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With