[英]Receive JSON Output from PHP in Android
I am using this code in my PHP script:我在我的 PHP 脚本中使用此代码:
echo json_encode($output);
and I get the following in my pages output:我在页面输出中得到以下内容:
[{"food_id":"1","food_description":"Free Range Egg","food_protein":"8"},{"food_id":"3","food_description":"Seeded Bread","food_protein":"6"}]
Can you please lead me the way on how I can receive the pages output in my Android Application and fit in in my object below?您能否指导我如何在我的 Android 应用程序中接收页面输出并适应我下面的对象? eg:例如:
food.setFood_id(c.getInt("food_id"));
You need to do something like this..你需要做这样的事情..
JSONObject jsonObject = new JSONObject(response);// response is your json data which you are sending from php
JSONArray jsonArray = jsonObject.getJSONArray("mMessage");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
food.setFood_id(jsonObj.getInt("food_id"));
}
and from php side you need to send data like this从 php 端你需要发送这样的数据
public function get_data()
{
$json_response = array();
$sql_querry_arry = array(); // array filled with sql result
$json_response['mMessage'] = $sql_querry_arry;
echo json_encode($json_response);
}
Now receive output as a string and follow the steps i mention before现在以字符串形式接收输出并按照我之前提到的步骤进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.