PHP对象Json编码由Android

时间:2016-08-18 23:19:06

标签: php json parsing

我有PHP代码

$avg_objs = $value['avg'];

我在Json编码,我得到:

"平均":23

但我需要得到:

"avg":{"AverageTemperature":"23"}

由于我将它用于Android,如果没有使用这种格式,我得到" org.json.JSONException:无法将响应解析为JSON数据"

感谢。

1 个答案:

答案 0 :(得分:2)

在你的PHP中

$json = array('avg' => array('AverageTemperature' => $value['avg']) );
$json_string = json_encode($json);
echo $json_string;

这将构建您需要的数据结构,json_encode($json);将确保从中创建有效的json字符串。