【发布时间】:2012-01-13 13:07:28
【问题描述】:
我在 java 中生成了一个包含 JSON 对象的 HTMLPost 请求,并希望在 PHP 中对其进行解析。
public static String transferJSON(JSONObject j) {
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response;
HttpPost httppost= new HttpPost(SERVERURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", j.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
}
在服务器上
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// input = "json=%7B%22locations%22%3A%5B%7B%22..."
$input = file_get_contents('php://input');
// jsonObj is empty, not working
$jsonObj = json_decode($input, true);
我猜这是因为 JSON 特殊字符被编码了。
json_decode 返回空响应
知道为什么吗?
【问题讨论】: