KingNeil Posted February 8, 2016 Share Posted February 8, 2016 (edited) I am using Javascript to encode data with json... and using PHP to decode this data with json_decode(); My JSON looks like this: {"0":{"url":"http://www.businessinsider.com/mark-zuckerberg-mandarin-lunar-new-year-video-2016-2#BusinessInsider","title":"Mark Zuckerberg shows off his Mandarin skills to wish a healthy and prosperous Year of the Monkey","source":"http://www.businessinsider.com","timestamp":1454909507},"1":{"url":"http://uk.businessinsider.com/mark-zuckerberg-mandarin-lunar-new-year-video-2016-2#BusinessInsider","title":"Mark Zuckerberg shows off his Mandarin skills to wish a healthy and prosperous Year of the Monkey","source":"http://uk.businessinsider.com/tech","timestamp":1454921864} When I do json_decode, it doesn't work The reason why, is because of the "0":, "1": etc Usually, json is not encoded with {"0":{"url":"http://www.businessinsider.com but rather, just {"url":"http://www.businessinsider.com without the "{0": What is your solution for this?? Can PHP decode the "{0": Or, is the solution to strip the "{0": altogether...? If so, how would I do this? Thanks Edited February 8, 2016 by KingNeil Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 8, 2016 Share Posted February 8, 2016 Try using http://jsonlint.com/ and validate your json Quote Link to comment Share on other sites More sharing options...
kicken Posted February 8, 2016 Share Posted February 8, 2016 The "0": and "1": are there because you encoded an array-like object. Such json is perfectly valid, and would decode just fine with json_decode. You'll probably want to pass the $assoc flag as true so it decodes to an array rather than an object, that will make accessing things easier. $array = json_decode($json, true); var_dump($array[0], $array[1]); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.