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?
// 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");
}
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
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);
Related
In one of my libraries say libA, I am reading a JsonArray (with string values in it) from JsonObject and converting it to String[] before adding it to an intent as extra and then I pass down that intent to another activity say activityA. I am reading from intent in activityA and creating a JsonObject out of it for use later, but am not able to add an array directly to a JsonObject.
Code in libA:
JsonObject metadata; // This has the JsonArray in it.
final String[] bookIdsArray = new Gson().fromJson(metadata.get("bookIds") , String[].class);
testIntent.putExtra("bookIds", bookIdsArray);
Code in activityA:
final JsonObject activityMetadata = new JsonObject();
activityMetadata.addProperty("bookIds", String.valueOf(intent.getStringArrayExtra("bookIds")));
addProperty method of JsonObject only accepts String, Number, Boolean or Character and another method called 'add' only accepts JsonElement. How can I directly add array to it?
Use the JsonObject method add(String property, JsonElement value) as JsonArray extends JsonElement.
String[] idsFromIntent = intent.getStringArrayExtra("bookIds");
JsonArray bookIds = new JsonArray();
for (String id : idsFromIntent) {
bookIds.add(id);
}
activityMetadata.add("bookIds", bookIds);
i need help to parse this json code to actual strings using android volley. this is the json code:[{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].
I have tried other helps but not working. Thanks!
Remember:
if the .json content starts with { is considered as a Json Object.
if the .json content starts with [ is considered as a Json Array.
so you hava a JsonArray then you can parse your content like this way:
//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject = jsonArray.getJSONObject(i);
}
// strData is the json data received.
JSONArray jsonArray = new JSONArray(strData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
String zero = jsonObject.optString("0");
String thread_name = jsonObject.optString("thread_name");
String one = jsonObject.optString("1");
String post = jsonObject.optString("post");
String two = jsonObject.optString("2");
String post_time = jsonObject.optString("post_time");
String three = jsonObject.optString("3");
//Just an example
arrayName.add(jsonObject.optString("name"));
}
You can even use array to store data instead of the string.
Currently I have this JSON structure:
{"title1":["info1","info2"],"title2":"info2","title3":"info3"}
What I need to achieve is this:
{"title4":[{"a":"aa","b": "bb"}]}
To achieve the above, I use JSONArray:
JSONArray list = new JSONArray();
list.put("info1");
list.put("info2");
obj.put("title1", list);
And JSONObject:
JSONObject obj = new JSONObject();
obj.put("title2", "info2");
How can I visualize the desired output?
To achieve what you want, a JSONObject with a JSONArray containing JSONObjects, you can use the following code
JSONObject root = new JSONObject(); // The root JSON object
JSONArray title4 = new JSONArray(); // The JSON array that will contain JSON objects
// The JSON objects
JSONObject a = new JSONObject();
a.put("a", "aa");
JSONObject b = new JSONObject();
b.put("b", "bb");
title4.put(a);
title4.put(b);
// Put the JSON array in the root JSON object
root.put("title4", title4);
How can I parse json from the URL? below is my json structure which does not has tags.
[{"channelId":"0465CDBE","channelName":"ATV2"},{"channelId":"06E6923B1","channelName":"Phoenix"},{"channelId":"07B4FB7ed","channelName":"N24"},{"channelId":"115B73E39","channelName":"ORF2"},
Simply get the JSONArray
JSONArray jArr = new JSONArray(jsonString);
for(int i=0;i<jArr.length;i++)
{
String jChannel = jArr.getJSONObject(i).getString("channelId");
String jChannelName = jArr.getJSONObject(i).getString("channelName");
//you can now play with these variables or add to some list or do whatever you like.
}
The outer object is a JSONArray, so you can iterate it with a "for".
Inside your json array, you have simple json objects. You can parse it by key or iterate the keys.
You can user json object and get the value with getJSONArray
examples:
testob = {"channelId":"07B4FB7ed","channelName":"N24"}, {"channelId":"115B73E39","channelName":"ORF2"},
JSONObject jsonObject = new JSONObject(testob);
JSONArray dataArray = jsonObject.getJSONArray("data");
JSONObject jsonProductData = dataArray.getJSONObject(0);
I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");