我正在尝试使用 Drupal 7 连接到 REST Web 服务。数据是通过 url 提供的,我想将其拉入 D7 站点以创建和填充节点。我正在使用提要模块来映射内容并将 JSONPath 解析器作为解析器。我还包含了 Feeds HTTPFetcher Append Headers 模块,以便我可以添加自定义标头,以确保返回的是 JSON,而不是我之前得到的格式错误的 xml。
自定义标题:
Accept|application/json
Content-Type|application/json
我有一个看起来像的网址http://192.136.0.31:8080/places/getAll
除上述内容外,我还安装了 Devel 模块,该模块使用我的 template.php 中的以下代码在页面中返回 feed 数组:
$request = drupal_http_request('http://192.136.0.31:8080/places/getAll', $options);
dpm(drupal_json_decode($request->data));
这是Devel返回的:
... (Array, 1192 elements)
0 (Array, 17 elements)
Address1 (String, 19 characters ) 1-2 The Road name
Address2 (String, 9 characters ) Shuinnad
我遇到的问题是将其放入提要并映射到相关字段。我不知道上下文字段应该做什么 - 我试过$...*
, $.*
, $...[*] 但没有运气。
当我从开发输出中单击一个 Array 元素时,它显示 $...[0]['Address1'] 这表明应该是上下文 - 不走运。
快速更新 - 使用 drupal_json_decode 函数,我可以将数组拆分出我需要如何使用 php
foreach ($json as $section => $items) {
foreach ($items as $key => $value) {
//if ($key == 'Address1') {
echo "$section:\t$key\t: $value<br>";
//}
// check whether the current item is an array!
if(is_array($value)) {
echo "$key is sub array:<br />";
foreach($value as $subKey => $subValue)
echo "$subKey:\t$subValue<br />";
}
}
}
问题仍然存在,如何使用 JSON 解析器在 Feed 中复制它?