I am trying to collect data from server in the form of JSON in android. But having some technical glitch, because I have to parse the JSON data which is NOT in the form of key:value pair. OR How to parse user created associative array that is created in JSON? Need Help? Thanking you in advance.
Edit
I want to parse the JSON object that I receive in either of the two cases below.
Case 1:
The PHP Script is as follows :
<?php
session_start();
$arraygive = $_SESSION['arraygive'];
$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");
$output = array();
$output1 = array();
foreach ($arraygive as $lid)
{
echo "<br>";echo "new pass";echo "<br>";
$query = "SELECT * FROM places WHERE(LID = '$lid');";
$result = mysql_query($query) or die(mysql_error());
$output = mysql_fetch_row($result);
array_push($output1, $output);
}
print(json_encode($output1));
?>
It creates a JSON object of the below format:
[["1","shopknock","0","0","22","18.5123","73.8563"],["2","Food Shopei","231","1","17","18.5122","73.8562"],["10","Ccd","0","0","22","18.5211","73.857"]]
I don't know how to parse this format at all in Android.
Case 2:
The PHP Script is as follows :
<?php
session_start();
$arraygive = $_SESSION['arraygive'];
$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");
$output = array();
$output1 = array();
foreach ($arraygive as $lid)
{
echo "<br>";echo "new pass";echo "<br>";
$query = "SELECT * FROM places WHERE(LID = '$lid');";
$result = mysql_query($query) or die(mysql_error());
$output = mysql_fetch_assoc($result);
array_push($output1, $output);
}
print(json_encode($output1));
?>
It creates a JSON object of the below format:
[{"lid":"1","name":"shopknock","rid":"0","cid":"0","ccnt":"22","locx":"18.5123","locy":"73.8563"},{"lid":"2","name":"Food Shopei","rid":"231","cid":"1","ccnt":"17","locx":"18.5122","locy":"73.8562"},{"lid":"10","name":"Ccd","rid":"0","cid":"0","ccnt":"22","locx":"18.5211","locy":"73.857"}]
Note that the only difference between Case1 and Case 2 PHP script is mysql_fetch_row and mysql_fetch_assoc
I am not able to parse this JSON object with the code I use (given below Case 3) although it works with Case 3.
Case 3: BUT Whenever a JSON object of this above format (Case 2) is created from another PHP script below :
<?php
//used for populating list of catagories at different instances
$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");
$query = "SELECT * FROM places ORDER BY ccnt DESC;";
$result=mysql_query($query) or die(mysql_error());
$output=array();
while($row=mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
?>
The JSON object works(when created from above script but is same as the one given in Case 2)
The Android code I'm using to parse the JSON object in Case 3 is as follows:
JSONArray jsonArray = new JSONArray(result);
int length = jsonArray.length();
for (int i = 0; i < length; i++)
{
JSONObject jObj = jsonArray.getJSONObject(i);
String name = jObj.getString(TAG_NAME);
String rid = jObj.getString(TAG_RID);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_RID, rid);
map.put(TAG_NAME, name);
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(User_Home_List_Activity.this, oslist,
R.layout.list_v, new String[] { TAG_NAME }, new int[] { R.id.name});
l1.setAdapter(adapter);
}
Edit
I tried debugging using the Toasts and I realized that in the code above (which is in a Try block) the control does not reach the first line itself. i.e.
JSONArray jsonArray = new JSONArray(result);
If I apply a Toast inside the Try block before this above line, then it is displayed(i.e.control reaches that line). But a Toast after this above line doesn't get displayed.