本文介绍了带有特殊字符的Json_decode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很大的问题,即通过jQuery Ajax将数据作为JSON发布到我的服务器. JSLint表示数据正常,并且请求的Content-Type设置为application/x-www-form-urlencoded; charset=UTF-8.服务器在PHP 5.2.11上运行,所以我不能使用json_last_error().

I got a big problem posting data via jQuery Ajax as JSON to my Server. JSLint say the data is OK and the Content-Type of the request is set to application/x-www-form-urlencoded; charset=UTF-8. Server runs on PHP 5.2.11 so I can't use json_last_error().

我尝试了url_decode,utf8_decode和html_entities_decode,但似乎无济于事.

I tried url_decode, utf8_decode and html_entities_decode, but nothing seems to work.

var_dump(json_decode($jdata));返回null,但是如果我执行var_dump($jdata),则一切正常. $jdata是发布数据:$jdata = $this->input->post('requestdata');.

var_dump(json_decode($jdata)); returns null, but if I do a var_dump($jdata) everything looks OK. $jdata is the post data:$jdata = $this->input->post('requestdata');.

以下是从Firebug获取数据的示例示例:

Here some example post data grab from 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);

结果是:

通过直接将JSON字符串粘贴到PHP源代码中,它可以工作,但不能从帖子中获取它!

By pasting the JSON string direct into the PHP source it works, but getting it from post not!

推荐答案

由于字符串中的新行而出现错误

You are having error because of new line in your string

$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)

这篇关于带有特殊字符的Json_decode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:39