-1
[{
    "ID": 1,
    "datum": "2016-02-04 14:20:59",
    "nummer": "+467123123951",
    "text": "Svar..."
}, {
    "ID": 2,
    "datum": "2016-02-05 10:11:36",
    "nummer": "+467123123951",
    "text": "BLOOG STOPP"
}]

This is the respose I get but I have problems converting it to array with decode-function in php. Is this json-string malformed?

How can I generate a php table or print it nicely.

would appreciate any help, thanks.

Edit: My php version is 5.6 The JSON-string seems to be valid according to JSONLint. I get the JSON from my service provider ($url) with the following php-code:

$in=file_get_contents($url."/in/?key=xxxxxxxxx");
$json = json_decode($in);
echo json_encode($json, JSON_PRETTY_PRINT);

and the response header:

Connection: close, Content-Encoding: gzip, Content-Length: 212, Content-Type: text/html, Date: Sat, 06 Feb 2016 07:25:07 GMT, Server: nginx, Vary: Accept-Encoding,User-Agent

Does this (Content-Encoding: gzip) mean that the response is compressed?

user1450458
  • 67
  • 1
  • 6

1 Answers1

1

Your JSON example is valid. json_decode() works in my example:

$json = '[{ "ID": 1, "datum": "2016-02-04 14:20:59", "nummer": "+467123123951", "text": "Svar..." }, { "ID": 2, "datum": "2016-02-05 10:11:36", "nummer": "+467123123951", "text": "BLOOG STOPP" }]';

echo "<pre>";
print_r(json_decode($json));
echo"</pre>";

As i think you cut the text, maybe your string is not utf-8 encoded? json_encode()/json_decode() needs utf-8 encoding as soon as there are special characters.

Update: I am not sure about the Content-Encoding - here is some further Information on this topic: Transfer-Encoding: gzip vs. Content-Encoding: gzip But did you verify that the string is UTF-8 encoded before performing the json_decode()?

Can you change your resource on $url."/in/?key=xxxxxxxxx" to just output a very simple JSON string like [{ "ID": 1, "text": "test"}] If the json_decode now works you know its not about gzip, if it doesnt, you know its not about UTF-8 (as the string has no special chars).

Community
  • 1
  • 1
Tarsis
  • 712
  • 6
  • 14
  • Yes, thank you. I get exactly the same response with your code. So I realized something else is wrong. I get the json-string response with the php function file_get_contents(url). But Im not able to json_decode the response. – user1450458 Feb 05 '16 at 14:11
  • Not able to decode in the sense!? Every JSON will have to decode with the function json_decode. Since JSON is validated in JSON Lint, no mistake in codes. You're committing a silly mistake. Paste your php code! – Anirudh Murali Feb 05 '16 at 14:36
  • You are probably right. I have updated my post above. – user1450458 Feb 06 '16 at 05:30