1

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.

2 Answers2

1

Try Like This

for (int i = 0; i < mJsonArray.length(); i++) {

    JSONObject mJsonObject = new JSONObject();
    mJsonObject = mJsonArray.getJSONObject(i);

    for (int j = 0; j < mJsonObject .length(); j++) {

     int Id = mJsonObject.getString("lid");
     String Name = mJsonObject.getString("name");
     .
     .
     .
   }
}
Karan Mavadhiya
  • 1,042
  • 8
  • 23
0

I have tried with your Json reponse , and Wola Its Working. This is code snippet that you can use to parse this Json string with your JSON output of Case 2

    StringBuilder output = new StringBuilder();
    JSONArray jArr;
    try
    {
        jArr = new JSONArray(jString);

        for (int i = 0; i < jArr.length(); i++)
        {
            JSONObject jObj = jArr.getJSONObject(i);
            output.append("\n\n");
            output.append("\n lid : " + jObj.getInt("lid"));
            output.append("\n name : " + jObj.getString("name"));
            output.append("\n rid : " + jObj.getInt("rid"));
            output.append("\n cid : " + jObj.getInt("cid"));
            output.append("\n ccnt : " + jObj.getInt("ccnt"));
            output.append("\n locx : " + jObj.getDouble("locx"));
            output.append("\n locy : " + jObj.getDouble("locy"));
        }
    }
    catch (JSONException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    tvText.setText(output.toString());
bGuruprasad.com
  • 362
  • 3
  • 10