Initial JSON
{
"data": [
{
"request": [
[
{
"name": "John""email": "John#gmail.com"
}
],
[
"name": "William""email": "William#gmail.com"
]
]
}
]
}
Corrected JSON as it was not passing through JSONLint
{
"data": [
{
"request": [
{
"name": "John",
"email": "John#gmail.com"
},
{
"name": "William",
"email": "William#gmail.com"
}
]
}
]
}
How to extract an array of JSON inside JSON array.I got error message as
json array cannot be converted into JSON Object.
Following code will help to parse your Json:
try {
JSONObject jsonData = new JSONObject(jsonObject);
JSONArray dataJsonArray = jsonData.getJSONArray("data");
JSONArray jsonRequest = dataJsonArray.getJSONObject(0)
.getJSONArray("request");
JSONArray mRequestChildJson = jsonRequest.getJSONArray(0);
JSONObject innerJson = mRequestChildJson.getJSONObject(0);
LogHandler.e("TAG",
"mRequestChildJson" + innerJson.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
don't forgot to add check of has Key and size of array.
may be you're on right track but casting it in object
JSONArray JArray = jObject.getJSONArray("data");
try {
String str = result.toString();
System.out.println("prov-request -->"+str);
JSONObject json=new JSONObject(str);
JSONArray jsonarray = json.getJSONArray("data");
for(int i=0;i<jsonarray.length();i++){
JSONObject jsonnew=jsonarray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("request");
for(int j=0;j<jsonarrayROW.length();j++){
JSONObject jsonObject=jsonarrayROW.getJSONObject(j);
date.add(jsonObject.getString("name"));
from.add(jsonObject.getString("email"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Related
I want to show list of currencies with currencyName and currencyCode,
but getting response like
{
"success": true,
"symbols": {
"AED": "United Arab Emirates Dirham",
"AFN": "Afghan Afghani"
}
}
and how can I convert this into,
{
"success": true,
"symbols": [{
"code": "AED",
"name": "United Arab Emirates Dirham"
},
{
"code": "AFN",
"name": "Afghan Afghani"
}
]
}
I want to show list in Android application.
Try this method :
JSONObject resultObject = new JSONObject();
try {
resultObject.put("success",true);
JSONArray jsonArray= new JSONArray();
JSONObject jsonObject=new JSONObject(yourJsonString);
JSONObject jsymbol= jsonObject.getJSONObject("symbols");
Iterator<String> iter = jsymbol.keys(); //This should be the iterator you want.
while(iter.hasNext()){
String key = iter.next();
String value = (String) jsymbol.get(key);
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("code",key);
jsonObject1.put("name",value);
jsonArray.put(jsonObject1);
}
resultObject.put("symbols",jsonArray);//This will give you result you want
} catch (JSONException e) {
e.printStackTrace();
}
I am parsing a json url: http://www.json-generator.com/api/json/get/bQmwsOmYeq?indent=2 but I failed to make it happen. I am getting an error. What's wrong with this code?
public void parseJsonResponse(String result) {
Log.i(TAG, result);
pageCount++;
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("name");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
CountryInfor country = new CountryInfor();
country.setId(jObject.getString("id"));
country.setName(jObject.getString("name"));
countries.add(country);
}
adapter.notifyDataSetChanged();
if (dialog != null) {
dialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Replace
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("name");
these two lines by
JSONArray jArray = new JSONArray(result);
Also change
country.setId(jObject.getString("id"));
by
country.setId(jObject.getInt("id") + "");
Because id is of type int not a String.
And it will work fine.
You are getting response like
[
{
"id": 1,
"name": "Vietnam"
},
{
"id": 2,
"name": "China"
},
{
"id": 3,
"name": "USA"
},
{
"id": 4,
"name": "England"
},
{
"id": 10,
"name": "Russia"
}
]
So the response is JSONArray and not JSONObject.
Hope it'll help.
Edit
#Override
protected String doInBackground(Void... params) {
// call load JSON from url method
if(loadJSON(this.url) != null) {
return loadJSON(this.url).toString();
}
Log.d("LoadJSONFromUrl", "Failed to get JSONObject.");
return null;
}
You are getting JSONObject null, so the error is.
- You should know what is NullPointerException
the issue is with this line
JSONArray jArray = json.getJSONArray("name");
There is no array with name name. it is failing here. Correct this part to obtain the array properly. Rest looks fine.
I cut out a lot of the unimportant stuff to keep this simple. I just want the url property.
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"posts": [
{
"photos": [
{
"original_size": {
"url": "LINK",
"width": 612,
"height": 612
String json = "...";
try {
JSONObject jsonObj = new JSONObject(json);
JSONObject respObj = jsonObj.getJSONObject("response");
JSONArray posts = respObj.getJSONArray("posts");
JSONObject firstPost = (JSONObject) posts.get(0);
JSONArray photos = firstPost.getJSONArray("photos");
JSONObject firstPhoto = (JSONObject) photos.get(0);
JSONObject photoSize = firstPhoto.getJSONObject("original_size");
String url = photoSize.getString("url");
Log.e("URL:", url );
} catch (JSONException e) {
e.printStackTrace();
}
how to get json data with android
my json like this:
[
{
"meta": {
"next_id": 30
}
},
{
"data": [
{
"category_id": "2",
"name": "Anniversary Facebook Status",
"count": "53"
},
{
"category_id": "4",
"name": "April Fool Status",
"count": "16"
},
{
"category_id": "79",
"name": "Wise Facebook Status",
"count": "90"
}
]
}
]
My code
JSONObject jsonobject = JSONfunctions.getJSONfromURL("URL");
JSONArray jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Log.i("ID", jsonobject.getString("category_id"));
Log.i("Name", jsonobject.getString("name"));
}
i want get "data" array.
please help if anybody know:
Thank You.
Try this.
JSONArray total_array = new JSONArray(string json);
JSONArray data_array = total_array.getJSONObject(1).getJSONArray("data");
for(int i = 0; i < data_array.length(); i++){
JSONObject json_data = data_array.getJSONObject(i);
String category_id = json_data.getString("category_id");
}
Probably this may help you :
JSONArray jsonArray;
try {
jsonArray = new JSONArray("YOUR JSON DATA");
Log.v("MAIN ACTIVITY", "JSON OBJECT :"+jsonArray.getJSONObject(0).getJSONObject("meta").getString("next_id"));
JSONArray dArray = jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dArray.length();i++)
{
JSONObject jsonObject = dArray.getJSONObject(i);
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("category_id"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("name"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("count"));
}
}
catch (JSONException e1) {
e1.printStackTrace();
}
to get json array
JSONArray jsonArray = new JSONArray(jsonStr);
JSONArray dataArray= jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dataArray.length();i++){
JSONObject json_data = dataArray.getJSONObject(i);
String category_id = json_data.getString("category_id");
String name = json_data.getString("name");
String count = json_data.getString("count");
}
For more info see Android JSON Parsing Tutorial
Look into the Gson library by Google. It's easier than parsing the fields manually with the included JSON lab.
I am creating json string but unable to give tag to JSONObject in nested json string.
Here is what I want
"User": [
{
"User1": {
"name": "name1",
"Address": "add1",
"user_detail": {
"Qualification": B.E,
"DOB": 11/2/1990,
}
}
},
{
"User2": {
"name": "name2",
"Address": "add2",
"user_detail": {
"Qualification": B.E,
"DOB": 11/2/1990,
}
}
}
}
]
And I have managed to get until here
{"User":[{"name":"name1","Address":"Add1"}, {"Qualification": "B.E", "DOB":"11/12/1990"}]}
But I am failed to add tag for JSONObject both for USer and user_details
Here is my code
try {
user = new JSONObject();
user.put("name", "name1");
user.put("Address", "B.E");
} catch (JSONException je) {
je.printStackTrace();
}
try {
userObj = new JSONObject();
userObj.put("User1", user);
jsonArray = new JSONArray();
jsonArray.put(user);
} catch (JSONException j) {
j.printStackTrace();
}
}
try {
users = new JSONObject();
users.put("User", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
The main thing is I am do not know, how to give tags to JSONObject.
You could just pass the desired JSON String to the JSONObject constructor to do the job. Take a look here
Current String Contain JSONArray of JSONObject's instead of JSONObject as root element. You can create Current Json String in java as:
JSONArray jsonArray = new JSONArray();
// User1 JSONObjects
user = new JSONObject();
user.put("name", "name1");
user.put("Address", "B.E");
user_obj_one = new JSONObject();
user_obj_one.put("User1",user);
//...same for User2...
...
user_obj_two = new JSONObject();
user_obj_two.put("User2",user_two);
//put in final array :
jsonArray.put(user_obj_one);
jsonArray.put(user_obj_two);
//....