2

我有以下形式的 json 响应:

[{
  "id": 425055,
  "title": "Foo"
}, {
  "id": 425038,
  "title": "Bar"
}, {
  "id": 425015,
  "title": "Narf"
}]

我使用 oboe.js 创建一个高地流:

const cruiseNidStream = _((push, next) => {
    oboe({
        url: 'http://fake.com/bar/overview,
        method: 'GET',
        headers: {
            'X-AUTH': 'some token',
        },
    }).node('.*', (overview) => {
        // I expect here to get an object having and id, title property
        push(null, overview.id);
    }).done((overview) => {
        push(null, _.nil);
    }).fail((reason) => {
        console.error(reason);
        push(null, _.nil);
    });
});

我的问题是我不知道使用什么模式 node 以便它匹配该数组的每个元素。目前,我通过当前设置获得的项目是所有对象和属性:

425055
Foo
{ id: 227709, title: 'Foo' }

如果响应将具有如下属性:

{
  'overview': [],
}

我本来可以用.overview.*.

4

2 回答 2

3

双簧管有两种匹配数据的方式,通过路径和通过鸭子类型。

通过鸭子打字

oboe('/data.json')
  .node('{id title}', function(x) {
    console.log('from duck-typing', x)
  })

按路径

oboe('/data.json')
  .node('!.*', function(x) {
    console.log('from path matching', x)
  })
  //or also valid
  .node('!.[*]', function(x) {
    console.log('from path matching', x)
  })

在路径示例中,请注意!字符。这指的是树的根节点,这种模式只会将你的三个对象,而不是它们自己的任何属性进一步向下嵌套。

我做了一个gomix,你可以在控制台中查看这个工作,还可以查看源代码

于 2016-12-14T19:09:57.803 回答
1

Oboe.js 支持鸭子类型:

.node('{id title}', (overview) => {
 }

请注意,我的 json 是平的,所以这是可行的。嵌套 json 的结果可能会有所不同。

于 2016-12-13T19:29:14.553 回答