Reputation: 1361
I want to send a JSON
list from one activity to other. I am doing in this way
For sending
List<JSONObject> jsonList = new ArrayList<JSONObject>();
Intent i = new Intent(getApplicationContext(), AdapterContent.class);
Bundle b = new Bundle();
b.putString("Array",jsonList.toString());
i.putExtras(b);
For receiving
Bundle b = getIntent().getExtras();
String Array=b.getString("Array");
Log.i("TAG" ,Array);
JSONObject jsonobj = new JSONObject(Array);
Log.i("Name" , String.valueOf(jsonobj.getString("Name")));
JSON Object
[
{"Name":"Area1"},
{"Name":"Area 2"}
]
But it is prompting
W/System.err:at com.example.data.mydata.onCreate
It is printing the Array
but not the Name
from JsonObj
Is anything wrong here?
Upvotes: 1
Views: 98
Reputation: 34380
Why do you need to send JsonList
. It's better to create a model class for your List. and make it parcelable. and send it to another activity
intent.putExtra(Key,yourModelClass);
Upvotes: 0
Reputation: 3388
you must cast the json string into json array not as json object
String Array = getIntent().getExtras().getString("Array");
Log.i("TAG" ,Array);
JSONArray jsonArr = new JSONArray(Array);
JSONObject jsonObj;
for(int i=0;i<jsonArr.length();i++){
jsonObj = jsonArr.getJSONObject(i);
Log.i("Name" , String.valueOf(jsonObj.getString("Name")));
}
Note: it is recommended to serialize or convert the data to parcelable while passing between activities
Upvotes: 0
Reputation: 37414
// make a json array
JSONArray jsonArr = new JSONArray(Array);
// empty containers for later use
JSONObject jobj;
String name=null;
// traverse all json object according to index of jsonarray
for(int i=0;i<jsonArr.length();i++){
// fetch jasonObject according to index
jobj=jsonArr.getJSONObject(i);
// get the name string from object and use it accordingly
name=jobj.optString("Name");
}
Upvotes: 2