我通过 jQuery Ajax 将数据作为 JSON 发布到我的服务器时遇到了一个大问题。JSLint 表示数据正常,请求的 Content-Type 设置为application/x-www-form-urlencoded; charset=UTF-8
. 服务器在 PHP 5.2.11 上运行,所以我不能使用json_last_error()
.
我尝试了 url_decode、utf8_decode 和 html_entities_decode,但似乎没有任何效果。
var_dump(json_decode($jdata));
返回null,但如果我执行var_dump($jdata)
一切看起来都不错。$jdata
是帖子数据:$jdata = $this->input->post('requestdata');
。
这里有一些来自 Firebug 的示例帖子数据抓取:
{
"projectnumber": "345",
"projecdescription": "345",
"articles": [
{
"position": 1,
"article_id": 677,
"online_text": "3 Behälter; Band I-III nach indiv. Stückliste, Sprache: DE - Sprache: de"
},
{
"position": 2,
"article_id": 678,
"online_text": "2 Behälter; Band I-III nach indiv. Stückliste, Sprache: ### - Sprache: en"
}
]
}
编辑:
我现在试过了:
$string = $this->input->post('requestdata');
var_dump($string);
$json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($string));
$json = json_decode($json);
var_dump($json);
结果是:
string(338) "{"projectnumber": "4444", "projecdescription": "4444", "articles": [{"position":1, "article_id": 676, "online_text": "### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE - Sprache: de"}, {"position":2, "article_id": 681, "online_text": "### Behälter; Band I-III nach indiv. Stückliste, Sprache: ### - Sprache: en"}]}" NULL
通过将 JSON 字符串直接粘贴到 PHP 源代码中,它可以工作,但不能从帖子中获取!