php 带有特殊字符的 Json_decode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12911536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json_decode with special chars
提问by fillibuster
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().
我在通过 jQuery Ajax 将数据作为 JSON 发布到我的服务器时遇到了一个大问题。JSLint 表示数据正常,并且请求的 Content-Type 设置为application/x-www-form-urlencoded; charset=UTF-8。服务器在 PHP 5.2.11 上运行,所以我不能使用json_last_error().
I tried url_decode, utf8_decode and html_entities_decode, but nothing seems to work.
我尝试了 url_decode、utf8_decode 和 html_entities_decode,但似乎没有任何效果。
var_dump(json_decode($jdata));returns null, but if I do a var_dump($jdata)everything looks OK. $jdatais the post data:$jdata = $this->input->post('requestdata');.
var_dump(json_decode($jdata));返回 null,但如果我做一个var_dump($jdata)一切看起来都不错。$jdata是帖子数据:$jdata = $this->input->post('requestdata');。
Here some example post data grab from Firebug:
下面是一些从 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"
}
]
}
Edit:
编辑:
I tried this now:
我现在试过这个:
$string = $this->input->post('requestdata');
var_dump($string);
$json = preg_replace('/,\s*([\]}])/m', '', utf8_encode($string));
$json = json_decode($json);
var_dump($json);
The result is:
结果是:
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
string(338) "{"projectnumber": "4444", "projecdescription": "4444", "articles": [{"position":1, "article_id": 676, "online_text": "### Beh?过滤器;Band I-III nach indiv。Stückliste, 语言:DE - 语言:de"}, {"position":2, "article_id": 681, "online_text": "### Beh?lter; Band I-III nach indiv。Stückliste,语言:### - 语言:en"}]}" NULL
By pasting the JSON string direct into the PHP source it works, but getting it from post not!
通过将 JSON 字符串直接粘贴到 PHP 源代码中,它可以工作,但不能从帖子中获取!
采纳答案by Baba
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);
Output
输出
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)
回答by Hafenkranich
Voting for the newline too
也为换行投票
json_decode_nice + keep linebreaks:
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*:/','"":',$json);
$json = preg_replace('/(,)\s*}$/','}',$json);
return json_decode($json,$assoc);
}
If you want to keep the linebreaks just escape the slash.
如果您想保留换行符,只需避开斜线即可。
You don't need utf-8 encode, if everything is set to utf-8 (header, db connection, etc)
如果所有内容都设置为 utf-8(标题、数据库连接等),则不需要 utf-8 编码
回答by sumit
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);

