Rose_xxi Rose_xxi - 23 days ago 9
Android Question

Android: Parse JSON Array PHP

Please help, I'm having a hard time parsing the

o_total
array, parsing
orders
in a ListView and is successful but when I'm trying to parse
o_total
in a textview it keeps throwing me No Value for
o_total


This is the JSON:

{"orders":[{"id":"5","ord_name":"Cafe Americano","ord_desc":"Hot Cafe Americano","ord_price":"60","ord_qty":"2","customer_id":"54fb83cb15e66","ord_num":"13211554fb83cb15e66","price_x_quan":"120.00","image":"http://192.168.43.52/MMOS/uploads/cafeamericano.jpg","subtotal":"","imgimg":"uploads/cafeamericano.jpg"},{"id":"8","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"2","customer_id":"54fb83cb15e66","ord_num":"13211554fb83cb15e66","price_x_quan":"30.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"9","ord_name":"Cafe Americano","ord_desc":"Hot Cafe Americano","ord_price":"60","ord_qty":"1","customer_id":"54fb83cb15e66","ord_num":"13211554fb83cb15e66","price_x_quan":"60.00","image":"http://192.168.43.52/MMOS/uploads/cafeamericano.jpg","subtotal":"","imgimg":"uploads/cafeamericano.jpg"}]}{"o_total":[{"total":"210"}]}


This is my PHP Code:

<?php
``mysql_connect('localhost','root','')or die ('No Connection');
mysql_select_db('dbmoms');

$sum=0;
$total = $sum;


$sql1 ="SELECT * FROM orders ORDER BY id desc LIMIT 1";
if($row=mysql_fetch_array(mysql_query($sql1))){
$order_id=$row['ord_num'];
}
$sql ="SELECT * FROM orders WHERE ord_num = '$order_id' ";


$result = mysql_query($sql);
$arr["orders"] = array();
while($row = mysql_fetch_assoc($result)){
$arr['orders'][]= $row ;
$sum = $sum+$row['price_x_quan'];



}
$arr1= array();
$arr1['o_total'][] = array('total' => "$sum" );


$json_encoded_string = json_encode($arr);
$json_encoded_string1 = json_encode($arr1);

$json_encoded_string = str_replace("\\/", '/', $json_encoded_string);


echo $json_encoded_string;
echo $json_encoded_string1;


?>


I want to show o_total in my Android:TextView please help.

@Override
protected JSONObject doInBackground(String... args) {
JSONParserConfirmation jParser = new JSONParserConfirmation();

// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();


try {
// Getting JSON Array
user = json.getJSONArray("o_total");
JSONObject c = user.getJSONObject(0);


// Storing JSON item in a Variable
String id = c.getString("total");


//Set JSON Data in TextView
i_pay.setText(id);


} catch (JSONException e) {
e.printStackTrace();
}


}

Answer

Your json is invalid. You can't have multiple roots in your json data.

$result = mysql_query($sql);
$arr["orders"] = array();
    while($row = mysql_fetch_assoc($result)){
    $arr['orders'][]= $row ;
    $sum = $sum+$row['price_x_quan'];
}
$arr['o_total'][] = array('total' => "$sum" );

$json_encoded_string = json_encode($arr);  

// not sure this is necessary
$json_encoded_string = str_replace("\\/", '/', $json_encoded_string);

echo $json_encoded_string;