16

我通过 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 源代码中,它可以工作,但不能从帖子中获取!

4

3 回答 3

16

由于字符串中的新行,您遇到错误

$string = '{"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"}]}';


$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);

输出

object(stdClass)[1]
  public 'projectnumber' => string '4444' (length=4)
  public 'projecdescription' => string '4444' (length=4)
  public 'articles' => 
    array
      0 => 
        object(stdClass)[2]
          public 'position' => int 1
          public 'article_id' => int 676
          public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE   - Sprache: de' (length=78)
      1 => 
        object(stdClass)[3]
          public 'position' => int 2
          public 'article_id' => int 681
          public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: ###   - Sprache: en' (length=79)
于 2012-10-16T10:29:53.963 回答
11

也为换行投票

json_decode_nice + 保留换行符:

function json_decode_nice($json, $assoc = TRUE){
    $json = str_replace("\n","\\n",$json);
    $json = str_replace("\r","",$json);
    $json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json);
    $json = preg_replace('/(,)\s*}$/','}',$json);
    return json_decode($json,$assoc);
}

如果您想保留换行符,只需转义斜线即可。

如果所有内容都设置为 utf-8(标头、数据库连接等),则不需要 utf-8 编码

于 2013-04-30T23:22:03.713 回答
2
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);
于 2016-01-21T17:46:32.377 回答