简体   繁体   English

如何在 Angular2 应用程序中匹配双簧管节点事件中的每个新行分隔的 json 对象?

[英]How to match each new line delimited json object in oboe's node-event in Angular2 app?

I have API that returns data like this:我有返回这样的数据的API:

{"t":"point","id":817315,"tableid":141,"classid":142,"state":0,"loc":[6850735.34375,24501674.0039063]}
{"t":"line","id":817314,"tableid":204,"classid":2102,"loc":[[6850335.8828125,24501476.50390625],[6850341.48828125,24501476.8828125],[6850362.171875,24501492.21484375],[6850387.4140625,24501508.86328125],[6850442.66796875,24501545.69921875],[6850502.34375,24501584.0078125],[6850558.3359375,24501619.37109375],[6850611.375,24501654.73046875],[6850671.05078125,24501693.04296875],[6850708.62109375,24501687.1484375],[6850735.34375,24501674.00390625]]}

With code like this:用这样的代码:

    oboe('http://localhost:19100/pn/api/v1/fetch?cgid=22&north=6853000.0&east=24505000&south=6850000.0&west=24500000.0')
    .node('*', (row) => {
        console.log(row);

        return oboe.drop;
    })
    .done(() => {

        return oboe.drop;
    })
    .fail((err) => {
        // error
        console.log('oboe fail ', err);
        return oboe.drop;
    });

We come to node-callback not with each line but with each separate value.我们不是针对每一行而是针对每个单独的值来进行节点回调。 Ie the value of row is 1st time "point", 2nd time 817315, 3rd time 141 and so on.即行的值是第 1 次“点”,第 2 次 817315,第 3 次 141 等等。

My goal is to have json object on each line to be read into an object.我的目标是在每一行都有一个 json 对象被读入一个对象。

I asked the similar question but because the 1st issue was api service CORS issue it became CORS question which i retitled according to that.我问了类似的问题,但因为第一个问题是 api 服务 CORS 问题,所以它变成了 CORS 问题,我据此重新命名。

This server response is a bad practice.此服务器响应是一种不好的做法。 Since your API is running on localhost I believe you can alter this response.由于您的 API 在 localhost 上运行,我相信您可以更改此响应。 Please have your API responding a JSON Array.请让您的 API 响应 JSON 数组。

[
{"t":"point","id":817315,"tableid":141,"classid":142,"state":0,"loc":[6850735.34375,24501674.0039063]},
{"t":"line","id":817314,"tableid":204,"classid":2102,"loc":[[6850335.8828125,24501476.50390625],[6850341.48828125,24501476.8828125],[6850362.171875,24501492.21484375],[6850387.4140625,24501508.86328125],[6850442.66796875,24501545.69921875],[6850502.34375,24501584.0078125],[6850558.3359375,24501619.37109375],[6850611.375,24501654.73046875],[6850671.05078125,24501693.04296875],[6850708.62109375,24501687.1484375],[6850735.34375,24501674.00390625]]}
]

This answers only to question: How to match each json object with node pattern?这仅回答以下问题:如何将每个 json 对象与节点模式匹配?

Each line representing json object is matched with pattern '!'代表 json 对象的每一行都与模式 '!' 匹配so the code is something like this:所以代码是这样的:

        oboe(url)
        .node('!', (row) => {
            console.log(row);
        })
        .on('end', () => {
            console.log('Stream finished');
        });

code above logs each json object and "Stream finished" when all json objects are handled.上面的代码在处理所有 json 对象时记录每个 json 对象和“流完成”。

The title and question is now modified to match this answer better and i'm probably asking another more specific question.现在修改了标题和问题以更好地匹配这个答案,我可能会问另一个更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM