【发布时间】:2014-05-13 10:11:56
【问题描述】:
我正在尝试使用 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 中的以下代码在页面中返回提要数组:
$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 中复制它?
【问题讨论】:
标签: json rest drupal feed drupal-feeds