Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

php - Json_decode with special chars

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().

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

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');.

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"
        }
    ]
}

Edit:

I tried this now:

$string = $this->input->post('requestdata');
var_dump($string);
$json = preg_replace('/,s*([]}])/m', '$1', 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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("/[
]+/", " ", $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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...