I want parse all "name" and "desc" from this json object inside another json object. i need another "for"?
{
"main": {
"details": [
{
"owner_name": "owner_name1",
"id": "id1",
"details2": {
"data": [
{
"name": "name1",
"desc": "my desc1",
},
{
"name": "name2",
"desc": "my desc2",
}
],
}
},
{
"owner_name": "owner_name2",
"id": "id2",
"details2": {
"data": [
{
"name": "name3",
"desc": "my desc3",
},
{
"name": "name4",
"desc": "my desc4",
}
]
}
}
]
}
}
and My java Code is:
JSONObject jsono = new JSONObject(data);
JSONObject mainObject = jsono.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object1 = jsonArray.getJSONObject(i);
JSONObject objectDetails2 = object1.getJSONObject("details2");
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject object = jsonArrayData.getJSONObject(j);
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
It only show First "data" result inside details2 not showing second details2 inside data..
Now my result is: "name1,dmy desc1,name2,my desc2"
i want all result like : "name1,dmy desc1,name2,my desc2,name3,my desc3,name4,my desc4"
How can i go again inside object and array? i need details from "name"
and "desc"
Get details2 from object JSONObject and then get data JSONArray:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// get details2 JSONObject
if(object.has("details2")){
if(!object.isNUll("details2")){
JSONObject objectDetails2 = object.getJSONObject("details2");
// get data JSONArray from objectDetails2
if(objectDetails2.has("data")){
if(!objectDetails2.isNUll("data")){
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
// iterate to jsonArrayData
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject objectInner = jsonArray.getJSONObject(j);
String strName=objectInner.optString("name");
....
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}
I think you should go into "details2" JSON Object like this :
JSONArray jsonArrayData=object.getJSONObject("details2").getJSONArray("data");
you have to get the object first and then parse it for the diffrent names and info
JSONArray jsonArrayData;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject("details");
jsonArrayData=object.getJSONArray("data");
for(int k=0;k<jsonArrayData.length();k++)
{
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
}
i will suggest you use GSON it is quite simple to use,
Related
Hello guys I have a tricky problem which I've been solving for hours.
This is my JSON response:
{
"DATA": [
{
"Name": "Aha",
"ListData": [
{
"ID": 1
},
{
"ID": 2
},
{
"ID": 3
}
]
}
]
}
This is what I've done so far:
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray jsonArray = jsonObj.getJSONArray("DATA");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject data = jsonArray.getJSONObject(i);
JSONArray arr = data.getJSONArray(Constants.LIST_DATA);
for(int j = 0; j < arr.length(); j++) {
JSONObject innerData = arr.getJSONObject(i);
int id = innerData.getInt(Constants.ID);
item = new HashMap<>();
item.put(Constants.ID, id);
itemList.add(item);
}
}
} catch(JSONException e) {
Log.e("JSONException", "" + e.toString());
}
But I'm only getting the correct size (3) of the array but I'm getting the same ID which 1. What seems to be wrong with my code? Any help would greatly appreciated. Thanks!
Result is something like this:
ID=1,ID=1,ID=1
Whereas my expected is:
ID=1,ID=2,ID=3
You should have used
JSONObject innerData = arr.getJSONObject(j);
you're doing second loop with j=0 and you're trying to getting JSONString from first loop.
I want parse details of "name" and "id" here is my json and android code
{
"main": {
"details": [
{
"name": "name1",
"id": "id1"
},
{
"name": "name2",
"id": "id2"
}
]
}
}
and my code is:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}
return true;
}
I want out put "id" and "name"
try Like this:
JSONObject totalObject = new JSONObject(result);
JSONObject mainObject = totalObject.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}
Follow by this way:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
String name = object.getString("name");
String id = object.getString("id");
}
return true;
}
main is JSONObject, details is JSONArray, so the correct code to declare jarray is the following:
JSONArray jarray = jsono.getJSONObject("main").getJSONArray("details");
You have a JSONObject "main", containing a JSONOArray "details". This means you have to call getJSONObject("main").getJSONArray("details");
If you are going to such parsing multiple times, I'd recommend you to take a look at Gson - it is much easier with it. You have to set a SerializableName attribute to the link and image fields of the Actor class and Gson will take care to map them with the values from the json.
how can I fetch this response from URL in android?
1. //main array
[
//array in main array
[
//object in inner array
{
//data to be fetched here
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "Allen"
}
],
//2nd array in main array
[
{
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "TestName"
}
]
]
try below code :-
try {
JSONArray ja = new JSONArray(ur string);
for (int i = 0; i < ja.length(); i++)
{
JSONArray ja1 = ja.getJSONArray(i);
for (int j = 0; j < ja1.length(); j++) {
JSONObject jo = ja1.getJSONObject(j);
String user_id = jo.getString("user_id");
String sr_no = jo.getString("sr_no");
String user_type = jo.getString("user_type");
String name = jo.getString("name");
}
}
} catch (Exception e) {
// TODO: handle exception
}
I am new in Json parsing. I am receiving Json data from my url which is given below:
[
[
{
"message": "hdjcjcjjckckckckvkckckck",
"timetoken": 14151866297757284
},
{
"message": "ufjfjfjcjfjchfjdhwjroritkgjcj",
"timetoken": 14151869212145692
},
{
"message": "udjfjfudjcyshdhsnfkfkvkf",
"timetoken": 14151869317015766
},
{
"message": "lvkifjsywjfwhsjvjdjfudjgidufkg",
"timetoken": 14151869404695072
},
{
"message": "ifjfydncydhsxhshfjdjejtlfudj",
"timetoken": 14151869494732788
},
{
"message": "22637485969473849506&#&#*%-&+",
"timetoken": 14151869589393336
},
{
"message": "jcjfjdueywjfusufig",
"timetoken": 14151869671994892
},
{
"message": "ofkdiriflfkkkfkdiidk",
"timetoken": 14151869775170644
},
{
"message": "testing",
"timetoken": 14151869895179728
},
{
"message": 1234567,
"timetoken": 14151869986900556
},
{
"message": 9877653,
"timetoken": 14151870106439620
},
{
"message": "zxcvbnmmkljgdaqerip",
"timetoken": 14151870236042386
}
],
14151866297757284,
14151870236042386
]
I am using this to break JsonArray data into different index like I want to display message and timetoken in separate lines in my activity, like this:
message: hdjcjcjjckckckckvkckckck
time token: 14151866297757284
message: 22637485969473849506&#&#*%-&+
time token: 14151869212145693
What should I have to do in the following line of codes:
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
for (int i = 0; i < jsonObj.length(); i++) {
}
I want to display it in my Textview as described above.
Try this one (not tested):
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
JSONArray array = jsonObj.getJSONArray(0);
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String mesage = item.getJsonString("message");
String timespan = item.getJsonString("timespan");
}
you should put json parsing into try-catch block:
try{
JSONArray res = new JSONArray(response.toString());
JSONArray jsonArray = res.getJSONArray(0);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String message = object.getString("message");
String token = object.getString("timetoken");
}
}catch(Exception e){
e.printStackTrace();
}
you have a double array, so you have two JSONArray. Actually, JSONArray usually marked by [] and JSONObject marked by {}.
In other words {}=JSONObject and []=JSONArray.
I have solved the problem. Actually the Json string has double Json Array and then Json Object.
I was doing a mistake to send an object of Json Array to Json Object. Now I have make an object of Json Array1 then send it to Json Array2 and then send the object of Json Array2 in Json Object.
The code is given below:
try {
JSONArray jsonObj = new JSONArray(message.toString());
JSONArray jArray = new JSONArray(jsonObj.get(0).toString());
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
String messageString=c.getString("message");
String timeString=c.getString("timetoken");
String abc = timeString;
}
}
I'm working on json parsing project. The error is showing as json array cant be converted into json object. Here is the json view.
{
"State Name": [
[
{
"state_name": "New South Wales (NSW)"
}
],
[
{
"state_name": "Western Australia (WA)"
}
]
]
}
java code is:
try {
JSONArray statesnames = json.getJSONArray(TAG_STATE_NAME);
Toast.makeText(getApplicationContext(), ""+statesnames.length(), 3000).show();
// looping through All Contacts
for(int i = 0; i < statesnames.length(); i++){
JSONObject c = statesnames.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
}
If you're trying to send both arrays in one response you could do that:
[
[{"state_name":"New South Wales (NSW)"}],
[{"state_name":"Western Australia (WA)"}]
]
Take your JSONObject first as
JSONObject result= new JSONObject(json);
JSONArray arr= result.getJSONArray("State Name");
Whole JSON string is a JSONArray:
getJSONArray(0) contains JSONObjects with state_name as below:
for (int i = 0; i < arr.length(); i++) {
JSONArray firstArry = arr.getJSONArray(i);
for (int j = 0; j < firstArry.length(); j++) {
JSONObject secondarr= firstArry.getJSONObject(j);
System.out.println("state_name result: "+secondarr.getString("state_name"));
}
}
Try this..
{
"State Name":[ --> JSONArray
[ --> JSONArray
{ --> JSONObject
"state_name":"New South Wales (NSW)"
}
],
[
{
"state_name":"Western Australia (WA)"
}
]
]
}
Example :
JSONObject json = new JSONObject(response);
JSONArray statesnames = json.getJSONArray("State Name");
for (int i1 = 0; i1 < statesnames.length(); i1++) {
JSONArray jsonarray = statesnames.getJSONArray(i1);
for (int i2 = 0; i2 < jsonarray.length(); i2++) {
JSONObject jsonobject = jsonarray.getJSONObject(i2);
System.out.println("state_name : "+jsonobject.getString("state_name"));
}
}