views:

977

answers:

2

Hello there! I've having an issue with consuming a particular feed for a client. They have given me a remote URL and the response is a JSON string like so:

{"affiliate": [
{"ID":"1", "COUNTRY":"EXAMPLE", "NETWORK":"EXAMPLE", "PRIMARY":"EXAMPLE"},
{"ID":"2", "EXAMPLE":"EXAMPLE", "COUNTRY":"EXAMPLE", "NETWORK":"EXAMPLE", "PRIMARY":"EXAMPLE"},
{"ID":"3", "TITLE":"EXAMPLE", "COUNTRY":"EXAMPLE", "NETWORK":"EXAMPLE", "PRIMARY":"EXAMPLE"}
]}

For example purposes, I've shrank the feed to show the format, but in actuality there are hundreds of affiliates. Anyway, I want to use PHP json_decode because in the end, I need these affiliates in an associative array.

I've got something like this, but I just end up getting the raw string, and json_decode doesn't actually parse it into an associative array.

$request_url = "http://exampleurl.com/feed"; //returns feed like above

$json = file_get_contents($request_url, true); //getting the file content

$decode = json_decode($json, true);

print_r($decode);

It seems like I need to maintain the "\n" characters in the feed itself, but these get stripped out when using:

file_get_contents

Anyway, I think you know what I'm after, I'm just not sure what I'm doing wrong. I appreciate the help in advance. I've tried using jquery with jsonp but it would be more ideal this way since I need to sort through the array afterward and it doesn't need to be asynchronous.

Acorn

+3  A: 

It is possible that your feed contains unicode text. Try:

$decode = json_decode(addslashes($json), true)


Update:

Solved the problem. There are instances of \'s in the json data which json_decode doesn't handle properly. To solve this you need to double escape the \. This is what I did.

<?php
error_reporting(E_ALL);
$request_url = 'http://midas.glam.com/publisher_directory_data?network=glam&amp;country=US&amp;publish=Y';

$json = file_get_contents($request_url);
$json = str_replace('\\', '\\\\', $json);

$decode = json_decode($json, true);

var_dump($decode);
MitMaro
Thanks for pointing that out, but that is actually what I had in my code. Sorry for the confusion. The result is that the print_r function spits out the raw JSON feed instead of the associative array. Any other ideas?
Acorn
Updated my answer, might not be the problem but its worth a try.
MitMaro
Tried this and it just spits out the slashes along with the rest of the JSON string.
Acorn
Would it have something to do with the fact that the feed isn't contained within () or [] ?
Acorn
I don't think so. I am still working in it.
MitMaro
It works fine when I copy the raw xml file into a string, so I think the problem is with `file_get_contents`.
MitMaro
Answer updated, that one was very tricky.
MitMaro
Thanks for this. The str_replace() worked. I appreciate the help!This has been plaguing me for a couple of days now.
Acorn
+2  A: 

You're data feed escapes single quotes (apostrophes) with a backslash (e.g. \'). The JSON spec doesn't say this should be done, and thus PHP doesn't behave correctly.

See: http://bugs.php.net/bug.php?id=42708

You can try replacing all \' with ':

$json = str_replace('\\\'', "'", $json);

before calling json_decode.

blt04
Oops, sorry, didn't see MitMaro already found the problem ;)
blt04