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;
}
}
Related
I have string json like this :
{
"listResult": {
"items": [
{
"id": "629047db-66d9-4986-ba3f-c75554198138",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/629047db-66d9-4986-ba3f-c75554198138/8cb69c17-0fdb-454c-bfb5-a5b9001a9d59.jpg"
},
{
"id": "fa872dc8-d2b3-4815-92ef-d90e903bc3d8",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/fa872dc8-d2b3-4815-92ef-d90e903bc3d8/c510c24f-5bfd-4a64-8851-a5b90017a38d.jpg"
}
],
"totalItems": 34,
"pageSize": 5,
"pageNumber": 1,
"totalPages": 7,
"searchTerm": null
}
}
I Try Parsing with code :
try {
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
int jumlah_list_data = items_obj.length();
if (jumlah_list_data > 0) {
for (int i = 0; i < jumlah_list_data; i++) {
JSONObject obj = items_obj.getJSONObject(i);
String id = obj.getString(Variabel.id);
String thumbnail = obj.getString(Variabel.thumbnail);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
But I getting Error :
org.json.JSONException: No value for items
So how to solve it ? sorry for my English
Instead of:
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
you should have:
JSONObject json = new JSONObject(response);
JSONObject listResult = json.getJSONObject(Variabel.listResult);
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
Accordingly to the json you posted, items is direct child of listResult, so you have to use the JSONObject with key listResult to retrieve it. Change
JSONArray items_obj = json.getJSONArray(Variabel.items);
with
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
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,
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
}
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 need to parse Json in Android, as a newbie in Json as well as in Android I am unable to do so here is the json String:
[
{
"chapter": "1. General",
"lessons": [
{
"lesson": "1.1 "
},
{
"lesson": "1.2"
},
{
"lesson": "1.3"
}
]
},
{
"chapter": "2.emergencies"
}
]
Here I just want to get the lessons array data. So any help will really be appreciated. Thanks
JSONObject jObject = new JSONObject(jsonString);
JSONObject chapObject = jObject.getJSONObject("chapter");
Log.d("Chapt", chapObject.getString("chapter"));
JSONArray lessonArray = popupObject.getJSONArray("lessons");
for (int i = 0; i < 3; i++) {
Log.d("Name", lessonArray.getJSONObject(i).getString("lesson").toString());
Log.d("Value", lessonArray.getJSONObject(i).getString("onclick").toString());
}
JSONObject chap2Object = jObject.getJSONObject("chapter");
Log.d("Chapt2", chapObject.getString("chapter"));
Use JSONObject and/or JSONArray. They can be created directly from strings, eg:
JSONObject json = new JSONObject(string);
Android includes a JSON parser: http://developer.android.com/reference/org/json/package-summary.html
String json = "{ ... }";
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("lessions");
for (int i = 0; i < array.length(); i++) {
String lession = array.getJSONObject(i).getString("lession");
}