Drupal 7 中的 JSON 提要上下文 [英] JSON feeds context within Drupal 7

查看:21
本文介绍了Drupal 7 中的 JSON 提要上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Drupal 7 连接到 REST Web 服务.数据是通过 url 提供的,我想将其拉入 D7 站点以创建和填充节点.我使用 feeds 模块来映射内容和 JSONPath 解析器作为解析器.我还包含了 Feeds HTTPFetcher Append Headers 模块,以便我可以添加自定义标头以确保它返回的是 JSON 而不是我之前得到的格式错误的 xml.

I'm trying to connect to a REST web service with Drupal 7. Data is supplied via a url and I want to pull it into the D7 site to create and populate nodes. I'm using the feeds module to map the content and the JSONPath parser as the parser. I have also included Feeds HTTPFetcher Append Headers module so that I can add custom headers in to make sure it is JSON being returned and not badly formatted xml which I was getting before.

自定义标题:

Accept|application/json
Content-Type|application/json

我有一个类似于 http://192.136.0.31:8080/places/getAll

除上述之外,我还安装了 Devel 模块,该模块使用我的 template.php 中的以下代码返回页面中的提要数组:

Further to the above I have the Devel module in place which is returning the feed array in the page using the code below in my template.php:

$request = drupal_http_request('http://192.136.0.31:8080/places/getAll', $options);
dpm(drupal_json_decode($request->data));

这就是 Devel 的回报:

This is what Devel returns:

... (Array, 1192 elements)
   0 (Array, 17 elements)
      Address1 (String, 19 characters ) 1-2 The Road name
      Address2 (String, 9 characters ) Shuinnad

我遇到的问题是将其放入提要并映射到相关字段.我不知道上下文字段中应该包含什么 - 我试过 $...*, $.*, $...[*] 但没有运气.

The problem I'm having is getting this into feeds and mapping to the relevant fields. I don't know what should go in the context field - I have tried $...*, $.*, $...[*] but no luck.

当我点击 devel 输出中的一个 Array 元素时,它会显示 $...[0]['Address1'] 这表明这应该是上下文 - 没有运气.

When I click on an Array element from the devel output it shows $...[0]['Address1'] which suggests that should be the context - no luck.

快速更新 - 使用 drupal_json_decode 函数,我可以将数组拆分为我需要如何使用 php

Quick Update - using the drupal_json_decode function I can split the array out how I need to using php

foreach ($json as $section => $items) {
foreach ($items as $key => $value) {
    //if ($key == 'Address1') {
    echo "$section:	$key	:&nbsp;$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:	$subValue<br />";
    }
}
}

问题仍然存在,我如何使用 JSON 解析器在 Feeds 中复制它?

The question still stands, how do I replicate that in Feeds using the JSON parser?

推荐答案

参见 本教程

Context:是放置表示数据的数组路径的 JSONPath 表达式的位置.例如,在我的文件中,这将是 $.albums.data.*.如果这是一个 PHP 数组,那基本上是

Context: is where you put the JSONPath expression that represents the path to the array representing your data. For instance, in my file, this would be $.albums.data.*. If this were a PHP array, that would be basically

foreach($facebook['albums']['data'] 作为 $value);

foreach($facebook['albums']['data'] as $value);

这篇关于Drupal 7 中的 JSON 提要上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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