how I can access data from a JSONArray in Android? - android

how I can access data from a JSONArray? It is this and contains this information:
"deadlines": [
{
"start": 1439539200,
"end": 1439542800
},
{
"start": 1440144000,
"end": 1440147600
},
{
"start": 0,
"end": 0
}
]
I need to have in a String tag each item "start". Thanks
EDIT
My code is this:
JSONArray array = moduleObject.specialForcedConf;
// array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}]
for (int j=0; j < array.length(); j++)
{
try
{
JSONObject obj = array.getJSONObject(j);
String start = obj.getString("start");
String end = obj.getString("end");
Log.e("", "start = " + start);
}
catch (JSONException e)
{
Log.e("", "error = " + e.getMessage());
}
}
I get this error:
"error = No value for start"

Do this
JSONArray array = moduleObject.specialForcedConf;
// array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}]
JSONArray jArray = array.getJSONObject(0).getJSONArray("deadlines");
for (int i = 0; i < jArray.length(); i++) // assuming your array is jArray
{
try
{
JSONObject obj = jArray.getJSONObject(i);
String start= obj.getString("start"); // store in an ArrayList
String end = obj.getString("end"); //// store in an ArrayList
}
catch (JSONException e)
{
// Error
}
}

JSONArray mJsonArray=new JSONArray("Deadlines");
for(int i=0;i<mJsonArray.length();i++){
JSONObject mJsonObject=new JSONObject(mJsonArray.get(i).toString));
String start = mJsonObject.optString("start","");
String end = mJsonObject.optString("end","");
}

Try this:
StringBuilder sb = new StringBuilder();
JSONArray arr = new JSONArray("deadlines");
for(int i=0;i<arr.length;i++){
JSONObject obj = arr.getJSONObject(i);
sb.append(obj.get("start").toString());
sb.append(",");
}
String strStartTag = sb;

JSONArray ja = new JSONArray(yourjsondata));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo_feed = new JSONObject(ja.get(i).toString());
String start = jo_feed.getString("start");
}

Related

getJSONArray inside another json array

I have the following JSON struncture:
{
"schedule":{
"day":[
{
"id":"Monday",
"items":[
{
},
{
}
]
},
{
"id":"Tuesday",
"items":[
{
},
{
}
]
}
]
}
}
And what I basically want to do is reach the items array inside the day array which is inside the schedule object.
But whenever I try to get the second JSON array, I get getJSONArray
(int) in JSONArray cannot be applied to (java.lang.String).
JSONObject baseJsonResponse = new JSONObject(dayJSON);
JSONArray dayArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day").getJSONArray("items");
You should use Two for loop respect to JSONArray.
JSONObject obj = new JSONObject(success);
JSONObject JOBJ_Schedule = obj.getJSONObject("schedule");
JSONArray schedule_Array = JOBJ_Schedule.getJSONArray("day");
for (int i = 0; i < schedule_Array.length(); i++)
{
JSONObject jOBJ = schedule_Array.getJSONObject(i);
JSONArray jArray = jOBJ.getJSONArray("items");
for (int j = 0; j < jArray.length(); j++)
{
JSONObject jOBJNEW = jArray.getJSONObject(j);
}
}
Looks like day is a list of objects. So basically, you'd have to do something like getJSONArray("day").get(0).getJSONArray("items").
you can try this
try {
JSONObject baseJsonResponse = new JSONObject("dayJSON");
JSONObject schedule= baseJsonResponse.getJSONObject("schedule");
JSONArray day=schedule.optJSONArray("day");
for (int i=0; i<day.length(); i++) {
JSONObject data = day.getJSONObject(i);
String id = data.getString("id");
JSONArray items = data.getJSONArray("items");
for (int j = 0; j < items.length(); j++) {
JSONObject data2 = day.getJSONObject(i);
String str = data2.getString("YOurkey");
Log.e("categories", str);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
According to the JSON you provided, baseJsonResponse.getJSONObject("schedule").getJSONArray("day") will return you a JSONArray instead of JSONObject.
There is a "item" JSONArray inside each "day" JSONObject. < May be this is the reason.
You can try
for(int i = 0 ; i < baseJsonResponse.getJSONObject("schedule").getJSONArray("day").length() ; i++){
JSONArray itemArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day")
.getJSONObject(i).getJSONArray("items");
}

Android get Index 1 out of range on parse json format

i'm trying to parse this below json format such as:
[
[
{
"mobileNumber":"<Censored>","contactUserId":"17",
"userEwallets":
[
{"accountNumber":"<Censored>"},
{"accountNumber":"<Censored>"},
{"accountNumber":"<Censored>"}
]
}
]
,
[
{
"mobileNumber":"<Censored>","contactUserId":"1",
"userEwallets":
[
{"accountNumber":"<Censored>"}
]
}
]
]
for parsing second json array of that as
[
{
"mobileNumber":"<Censored>",
"contactUserId":"1",
"userEwallets":
[
{"accountNumber":"<Censored>"}
]
}
]
i get this error:
Index 1 out of range [0..1)
from below code my code can only parse the first array of that, for second array i get exception when i try to get mobileNumber of second json array object
for (int i = 0; i < response.length(); i++) {
try {
JSONArray jsonArray = response.getJSONArray(i);
final String mobileNumber = jsonArray.getJSONObject(i).getString("mobileNumber");
final String contactUserId = jsonArray.getJSONObject(i).getString("contactUserId");
final String userEwallets = jsonArray.getJSONObject(i).getString("userEwallets");
Log.e("MobileNumber ", mobileNumber);
JSONArray ewallets = new JSONArray(userEwallets);
for (int j = 0; j < ewallets.length(); j++) {
JSONObject ewalletObject = ewallets.getJSONObject(j);
final String accountNumber = ewalletObject.getString("accountNumber");
Log.e("accountNumber ", accountNumber);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Try this one...
JSONArray response;
try {
response = new JSONArray(res);
for (int i = 0; i < response.length(); i++) {
JSONArray insideJSONArray = response.getJSONArray(i);
JSONObject jsonObject = insideJSONArray.getJSONObject(0);
String mobileNumber = jsonObject.getString("mobileNumber");
Log.e("TAG", "mobileNumber: " + mobileNumber);
String contactUserId = jsonObject.getString("contactUserId");
Log.e("TAG", "mobileNumber: " + contactUserId);
JSONArray userEwallets = jsonObject.getJSONArray("userEwallets");
for (int j = 0; j < userEwallets.length(); j++) {
JSONObject ewalletObject = userEwallets.getJSONObject(j);
final String accountNumber = ewalletObject.getString("accountNumber");
Log.e("accountNumber ", accountNumber);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Change:
final String mobileNumber = jsonArray.getJSONObject(i).getString("mobileNumber");
final String contactUserId = jsonArray.getJSONObject(i).getString("contactUserId");
final String userEwallets = jsonArray.getJSONObject(i).getString("userEwallets");
to
final String mobileNumber = jsonArray.getJSONObject(0).getString("mobileNumber");
final String contactUserId = jsonArray.getJSONObject(0).getString("contactUserId");
final String userEwallets = jsonArray.getJSONObject(0).getString("userEwallets");

Parsing JSON in Android; arrayList_ph is null

Please help me to parse the following JSON data given below:
{
posts: [
{
count: 1,
user_id: "1",
name: "Dave Greeneberg",
email: "daveneberg#example.com",
profile_photo: "http://phontest.lbch.com//users/user1.jpg",
contest_count: "3",
photo_count: 19,
win_count: "0",
photos: [
"images/contest/diwali1.jpg",
"images/contest/diwalc2.jpg",
"images/contest/145043cd811.png",
"images/contest/145043def03411.jpg",
"images/contest/14504ger11.jpg"
]
}
]
}
I tried the following code but values in arrayList_ph is null. I am confused about how to parse this JSON content.
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = arr.getJSONObject(0).getString("name");
user_email = arr.getJSONObject(0).getString("email");
user_profile = arr.getJSONObject(0).getString("profile_photo");
user_count = arr.getJSONObject(0).getString("count");
user_photo_count = arr.getJSONObject(0).getInt("photo_count");
contest_count = arr.getJSONObject(0).getString("contest_count");
win_count = arr.getJSONObject(0).getString("win_count");
JSONArray ph_arr= arr.getJSONObject(0).getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
arrayList_ph.add(ph_arr.getString(in));
}
}
Please help me to parse that field.
Please try this solution.
This solutions is worked for me.
JSONObject object = new JSONObject("");
JSONArray arr = object.optJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.optJSONObject(index);
user = object1.optString("name");
user_email = object1.optString("email");
user_profile = object1.optString("profile_photo");
user_count = object1.optString("count");
user_photo_count = object1.optInt("photo_count");
contest_count = object1.optString("contest_count");
win_count = object1.optString("win_count");
JSONArray ph_arr = object1.optJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.opt(in).toString();
arrayList_ph.add(str);
}
}
change your parsing code to this ,
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getString("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray ph_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Try -
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts”);
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getInt("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray photos_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Please cross check Following:
1) I think your json response is not properly formatted.
Validate from this: https://jsonformatter.curiousconcept.com/
Your json response should be :
{
"posts":[
{
"count":1,
"user_id":"1",
"name":"Dave Greeneberg",
"email":"daveneberg#example.com",
"profile_photo":"http://phontest.lbch.com//users/user1.jpg",
"contest_count":"3",
"photo_count":19,
"win_count":"0",
"photos":[
{
"image":"images/contest/diwali1.jpg"
},
{
"image":"images/contest/diwalc2.jpg"
},
{
"image":"images/contest/145043cd811.png"
},
{
"image":"images/contest/145043def03411.jpg"
},
{
"image":"images/contest/14504ger11.jpg"
}
]
}
]
}
2) If that still does not work...Try debugging and post your log please...

Parsing JSON for android the JSON is valid but cant get anything back

I'm having trouble getting the String "lf" in this case North Atlantic Treaty Organization
[
{
"sf": "NATO",
"lfs": [
{
"lf": "North Atlantic Treaty Organization",
"freq": 13,
"since": 2001,
"vars": [
{
"lf": "North Atlantic Treaty Organization",
"freq": 13,
"since": 2001
}
]
}
]
}
]
//MY CODE
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("lf");
acronyms = new ArrayList<>();
acronyms.add(name);
}
} catch (Exception e) {
}
try {
JSONArray jsonArray1 = new JSONArray(JsonString);
for (int i = 0; i <= jsonArray1.length(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
String mStrSf = jsonObject2.getString("sf");
Toast.makeText(getApplicationContext(),mStrSf.toString(), Toast.LENGTH_LONG).show();
JSONArray jsonArray3 = jsonObject2.getJSONArray("lfs");
for (int j = 0; j <= jsonArray3.length(); j++) {
JSONObject jsonObject4 = jsonArray3.getJSONObject(j);
String mStrIf = jsonObject4.getString("lf");
String mStrFreq = jsonObject4.getString("freq");
String mStrSince = jsonObject4.getString("since");
Toast.makeText(getApplicationContext(),mStrIf+"\n"+mStrFreq+"\n"+mStrSince, Toast.LENGTH_LONG).show();
JSONArray jsonArray5 = jsonObject4.getJSONArray("vars");
for (int k = 0; k <= jsonArray5.length(); k++) {
JSONObject jsonObject6 = jsonArray5.getJSONObject(k);
String mStrIf1 = jsonObject6.getString("lf");
String mStrFreq1 = jsonObject6.getString("freq");
String mStrSince1 = jsonObject6.getString("since");
Toast.makeText(getApplicationContext(),mStrIf1+"\n"+mStrFreq1+"\n"+mStrSince1, Toast.LENGTH_LONG).show();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
You are interpreting the json in a wrong way... if you analyse the json chain you will get this:
as you can see you have an array inside and array...
hope the picture helps you to see it better.
You have to again loop through the jsonArray to get "lf" key value.
Try this...
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
JSONArray array = c.getJSONArray("lfs");
for(int j=0;j<array.length();j++){
JSONObject obj = array.getJSONObject(j);
String name = obj.getString("lf");
}
}
} catch (Exception e) {
}

Nested JSONArray Record Parsing Issue Android

I am making a App which receives a JSON Data From Server, User will select a value from Spinner like 0,1,2... and so on, On the Basis of number selection JSON will return a data from user defined index like 0,1.. i don't know how to parse a inner JSON Data in Android
json = new JSONObject(JSONParser.Result);
JSONArray jArray = json.getJSONArray("data");
StopElement _stop = new StopElement();
Log.d("JSON Algo Result", json.toString());
if (jArray!=null) {
for (int i = 0; i < jArray.length(); i++) {
jsonarray = jArray.getJSONArray(i);
if (jsonarray != null) {
for (int j = 0; j < jsonarray.length(); i++){
if(i==0) {
jsonarray = jArray.getJSONArray(j);
_stop.setName(jsonarray.getString(0));
StopElement.Stop_name_list.add(_stop.getName());
}
}
} else {
break;
}
}
} else {
return null;
}
**JSON Data**
[[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Petrol Pump","33.634109","73.076363","suzk1"],["Chandni Chowk","33.631584","73.072563","suzk1"],["Rahmanabad","33.639065","73.075714","3"],["Passport Office","33.642410","73.076981","3"],["Shamsabad","33.650101","73.079994","3"],["Faizabad","33.663212","73.084801","3"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","124-A"],["Kuri Road","33.643162","73.102928","124-A"],["Dhok Kala Khan","33.653118","73.095444","124-A"],["Faizabad","33.663212","73.084801","124-A"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","136"],["Kuri Road","33.643162","73.102928","136"],["Iqbal Town","33.644279","73.100113","136"],["Dhok Kala Khan","33.653118","73.095444","136"],["Faizabad","33.663212","73.084801","136"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Dhok Ali Akbar","33.636997","73.092117","suzk13"],["Highway Stop","33.679722","73.075584","suzk13"],["Bhinder","33.556244","73.167946","suzk3"],["Lohi Bher","33.586273","73.145493","124"],["Wild Life Park","33.578770","73.132309","124"],["Airport Chowk","33.593803","73.139938","124"],["Gangal","33.612591","73.125801","124"],["Khana Bridge","33.629967","73.112823","124"],["Zia Masjid","33.637196","73.107407","124"],["Kuri Road","33.643162","73.102928","124"],["Dhok Kala Khan","33.653118","73.095444","124"],["Faizabad","33.663212","73.084801","124"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Marir Hassan","33.596905","73.064445","suzk7"],["Punjab House","33.592701","73.065453","suzk7"],["Jhanda","33.588970","73.076195","suzk7"],["Raheemabad","33.599159","73.080048","21"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Medical College","33.602753","73.067200","29"],["Sir Syed Boys School","33.609600","73.078766","29"],["Fauji Tower","33.606770","73.084106","29"],["Chaklala Station","33.601013","73.095924","29"],["Raheemabad","33.599159","73.080048","29"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"]]]
this will work definitely for you.
String data= "[[['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.628395','73.101936','suzk1']]]";
try {
JSONArray json = new JSONArray(data);
JSONArray aray = (JSONArray)json.get(0);
for(int i =0; i< aray.length();i++){
JSONArray object = (JSONArray)aray.get(i);
String title = (String)object .get(0);
String lat = (String)object.get(1);
String lng = (String)object.get(2);
String com = (String)object.get(3);
}
} catch (JSONException e) {
e.printStackTrace();
}
try this way,
U can get result as per your request
private String jsonString="[[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Petrol Pump','33.634109','73.076363','suzk1'],['Chandni Chowk','33.631584','73.072563','suzk1'],['Rahmanabad','33.639065','73.075714','3'],['Passport Office','33.642410','73.076981','3'],['Shamsabad','33.650101','73.079994','3'],['Faizabad','33.663212','73.084801','3']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','124-A'],['Kuri Road','33.643162','73.102928','124-A'],['Dhok Kala Khan','33.653118','73.095444','124-A'],['Faizabad','33.663212','73.084801','124-A']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','136'],['Kuri Road','33.643162','73.102928','136'],['Iqbal Town','33.644279','73.100113','136'],['Dhok Kala Khan','33.653118','73.095444','136'],['Faizabad','33.663212','73.084801','136']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Dhok Ali Akbar','33.636997','73.092117','suzk13'],['Highway Stop','33.679722','73.075584','suzk13'],['Bhinder','33.556244','73.167946','suzk3'],['Lohi Bher','33.586273','73.145493','124'],['Wild Life Park','33.578770','73.132309','124'],['Airport Chowk','33.593803','73.139938','124'],['Gangal','33.612591','73.125801','124'],['Khana Bridge','33.629967','73.112823','124'],['Zia Masjid','33.637196','73.107407','124'],['Kuri Road','33.643162','73.102928','124'],['Dhok Kala Khan','33.653118','73.095444','124'],['Faizabad','33.663212','73.084801','124']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Marir Hassan','33.596905','73.064445','suzk7'],['Punjab House','33.592701','73.065453','suzk7'],['Jhanda','33.588970','73.076195','suzk7'],['Raheemabad','33.599159','73.080048','21'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Medical College','33.602753','73.067200','29'],['Sir Syed Boys School','33.609600','73.078766','29'],['Fauji Tower','33.606770','73.084106','29'],['Chaklala Station','33.601013','73.095924','29'],['Raheemabad','33.599159','73.080048','29'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13']]]";
private void getJSONResult(String jsonString) {
// TODO Auto-generated method stub
try {
JSONArray jsonArray = new JSONArray(jsonString);
// i.e. the JsonMainArray
JSONArray JsonInnerArray= (JSONArray)jsonArray.get(0);
// i.e. the JsonInnerArray
for(int i =0; i< JsonInnerArray.length();i++){
JSONArray jsonObject = (JSONArray)JsonInnerArray.get(i);
//i.e. the jsonObject of JsonInnerArray
String title = (String)jsonObject .get(0);
String latitude = (String)jsonObject.get(1);
String longitude = (String)jsonObject.get(2);
String companyName = (String)jsonObject.get(3);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I Solved My Issue like this
try {
int count = 0;
if (success == 1) {
json = new JSONObject(JSONParser.Result);
jArray = json.getJSONArray("data");
StopElement _stop = new StopElement();
for (int i = 0; i < jArray.length(); i++) {
count = count + 1;
// Log.d("Counter", String.valueOf(count));
get_path.add(String.valueOf(count));
jsonarray = jArray.getJSONArray(i);
Log.d("path Array", jsonarray.toString());
if (getitemno>0) {
getpath = jArray.getJSONArray(getitemno);
for (int j = 0; j < getpath.length(); j++) {
getresult = getpath.getJSONArray(j);
_stop.setName(getresult.getString(0));
_map.setLat(getresult.getDouble(1));
_map.setLng(getresult.getDouble(2));
StopElement.Stop_name_list.add(_stop.getName());
MapCoordinatesElement.Lat.add(_map.getLat());
MapCoordinatesElement.Lng.add(_map.getLng());
//Log.d("Stop names", _stop.getName());
// Log.d("Index Array", getpath.toString());
}
} else {
getpath = jArray.getJSONArray(0);
for (int j = 0; j < getpath.length(); j++) {
getresult = getpath.getJSONArray(j);
_stop.setName(getresult.getString(0));
_map.setLat(getresult.getDouble(1));
_map.setLng(getresult.getDouble(2));
StopElement.Stop_name_list.add(_stop.getName());
MapCoordinatesElement.Lat.add(_map.getLat());
MapCoordinatesElement.Lng.add(_map.getLng());
//Log.d("Stop names", _stop.getName());
//Log.d("Index Array", getpath.toString());
}
}
}
// Log.d("Stop names of Algo", StopElement.Stop_name_list.toString());
// Log.d("Index Stop names", StopElement.Stop_name_list.toString());
} catch (JSONException e) {
e.printStackTrace();
}

Categories

Resources