1

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

1
  • I noticed you changes the code. I ran the code with the correct print_r and it worked fine with the sample data. Could you provide the feed you are using? Commented Jul 29, 2009 at 14:41

2 Answers 2

5

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&country=US&publish=Y';

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

$decode = json_decode($json, true);

var_dump($decode);
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Updated my answer, might not be the problem but its worth a try.
Tried this and it just spits out the slashes along with the rest of the JSON string.
Would it have something to do with the fact that the feed isn't contained within () or [] ?
It works fine when I copy the raw xml file into a string, so I think the problem is with file_get_contents.
|
4

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.

Comments

Your Answer

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.