2012-10-16 55 views
12

我有一个很大的问题,通过jQuery Ajax发布数据作为JSON到我的服务器。 JSLint表示数据正常,请求的内容类型设置为application/x-www-form-urlencoded; charset=UTF-8。服务器运行在PHP 5.2.11上,所以我不能使用json_last_error()具有特殊字符的Json_decode

我试过url_decode,utf8_decode和html_entities_decode,但似乎没有任何工作。

var_dump(json_decode($jdata));返回null,但如果我做了var_dump($jdata)一切看起来都OK。 $jdata是发布数据:$jdata = $this->input->post('requestdata');

这里是一些例子后的数据从萤火虫抢:

{ 
    "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); 

结果是:

字符串( 338)“{”projectnumber“:”4444“,”projecdescription“:” 4444“,”articles“:[{”position“:1,”article_id“:676,”online_text“:”###Behälter;乐队I-III每个人。 Sprache:de“},{”position“:2,”article_id“:681,”online_text“:”###Behälter;乐队I-III每个人。 Stückliste,语言:### - 语言:恩 “}]}” NULL

通过粘贴JSON字符串直接到它的工作原理PHP源代码,而是从后没有得到它!

+0

这会为你工作:http://stackoverflow.com/一个/ 12884807/1226894 – Baba

+0

可能重复[json \ _decode在php中返回null](http://stackoverflow.com/questions/12884802/json-decode-is-returning-null-in-php) – Baba

+0

你确定你在服务器端有一个UTF8字符串?试试'var_dump(json_decode(utf8_encode($ jdata)));' – ThiefMaster

回答

9

你是有原因的字符串中的新行错误

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

您让我的一天! – fillibuster

+0

@grolle很高兴知道..谢谢:) – Baba

9

投票的换行符太

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); 
} 

如果你想保持linebreaks只是逃避斜线。

你不需要UTF-8编码,如果一切都设置为UTF-8(头,数据库连接等)

+2

不要忘记在淘气人物名单中加入\ t。这似乎只是PHP <5.4的问题。 – mtutty

+0

@mtutty,感谢您的评论! – Hafenkranich

+1

@RaphaelWeber谢谢你的兄弟,你节省了我的时间 –

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