Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 775
fillibuster Avatar asked Oct 16 '12 09:10

fillibuster


People also ask

How to decode JSON stringify in PHP?

you can read in php with : $data = json_decode($_POST['data'],true); echo $data[1][1]; use logic to cycle through data.

What does JSON decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.


3 Answers

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)
like image 131
Baba Avatar answered Sep 19 '22 14:09

Baba


Voting for the newline too

json_decode_nice + keep linebreaks:

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

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)

like image 36
Hafenkranich Avatar answered Sep 22 '22 14:09

Hafenkranich


$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);
like image 21
sumit Avatar answered Sep 21 '22 14:09

sumit