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!
you can read in php with : $data = json_decode($_POST['data'],true); echo $data[1][1]; use logic to cycle through data.
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.
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)
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)
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With