帮助消耗JSON饲料用PHP和放大器; json_de code [英] Help with consuming JSON feed with PHP & json_decode

查看:108
本文介绍了帮助消耗JSON饲料用PHP和放大器; json_de code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,耗费了特定订阅的客户端。他们给了我一个远程URL和响应是一个JSON字符串,如下所示:

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"}
]}

例如而言,我已经缩小了进料,以显示格式,但实际上有几百个分支机构。无论如何,我想用PHP json_de code,因为在最后,我在一个关联数组需要这些分支机构。

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.

我有这样的事情,但我只是最终得到的原始字符串,json_de code实际上并没有解析成一个关联数组。

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);

好像我需要保持\ N字饲料本身,但在使用这些得到剥离出来:

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

file_get_contents

不管怎样,我想你知道我后,我只是不知道我做错了。我AP preciate提前帮助。我试着使用jQuery与JSONP,但它会更理想的这种方式,因为我需要通过数组排序后,它并不需要是异步的。

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.

橡果

推荐答案

这可能是你的饲料中含有单code文本。尝试:

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

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



更新:

问题解决了。也有 \的在JSON数据, json_de code 不正确处理实例。为了解决这个问题,你需要加倍逃避 \ 。这是我做的。

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);

这篇关于帮助消耗JSON饲料用PHP和放大器; json_de code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆