我正在尝试使用 Drupal 7 连接到 REST Web 服务。数据是通过 url 提供的,我想将其拉入 D7 站点以创建和填充节点。我正在使用提要模块来映射内容,并使用 JSONPath 解析器作为解析器。我还包含了 Feeds HTTPFetcher Append Headers 模块,这样我就可以添加自定义 header ,以确保返回的是 JSON 而不是我之前得到的格式错误的 xml。
自定义 header :
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
我遇到的问题是将其放入提要并映射到相关字段。我不知道上下文字段中应该放什么 - 我试过 $...*
、$.*
、$...[*] 但没有成功.
当我从 devel 输出中单击数组元素时,它显示 $...[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 解析器在 Feeds 中复制它?
最佳答案
您可能需要尝试这个模块:Feeds JSONPath Parser
关于Drupal 7 中的 JSON 提要上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628404/