请帮忙,我很难解析o_total
数组,在ListView中解析orders
,并且很成功,但当我尝试在文本视图中解析o_total
时,它一直给我抛出o_total
的No Value
这是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"}]}
这是我的PHP代码:
<?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;
?>
我想在我的Android中显示o_total:TextView请帮忙。
@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();
}
}
您的json无效。json数据中不能有多个根。
$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;