0

I am trying to parse the following json that I got through a web service in php.

$string = [{xxx:"xxx",yyy:"yyy",zzz:"zzz"}, {xxx:"xxx",yyy:"yyy",zzz:"zzz"}];

I try to parse it as with json_decode but it doesn't work.

$json = json_decode($string);

Returns nothing. What should I do?

asked Jan 6, 2012 at 0:44
3
  • Have you turned error reporting on in PHP? Have you tried print_r($json)? Commented Jan 6, 2012 at 0:45
  • 1
    What you show above is not valid JSON, the $string = breaks it. Is it part of your response? Commented Jan 6, 2012 at 0:46
  • 2
    That's not JSON. It's a Javascript expression. Try the search, this came up before. Commented Jan 6, 2012 at 0:48

1 Answer 1

1

That's not valid JSON. First of all, the entire thing needs to be a string. Use single-quotes here. Then, every name/value within the string needs to be double-quoted. Like so:

<?php
$string = '{ "one": {"xxx": "xxx", "yyy": "yyy", "zzz": "zzz"}, "two": {"xxx": "xxx", "yyy": "yyy", "zzz": "zzz"}}';
$json = json_decode($string);
var_dump($json);
?>

http://codepad.org/sOeEfOnr
http://php.net/manual/en/function.json-decode.php

answered Jan 6, 2012 at 0:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.