json - NodeJS 从流中反序列化

标签 json node.js stream deserialization

我在从 Node 中的流(特别是来自 Bitcoin GOX 交易所的定价源)反序列化时遇到问题。基本上,到达的 block 是格式良好、完整且经过验证的 JSON。这是代码:

var gox = require('goxstream');
var fs = require('fs');
var options = {
    currency: 'AUD',
    ticker: true,
    depth: false
};

var goxStream = gox.createStream(options);
goxStream.on('data', function(chunk) {

    console.log(JSON.parse(chunk));
});

当尝试解析它时,我得到以下内容

undefined:0

^
SyntaxError: Unexpected end of input

有什么想法吗?我已经包含了一个示例 block :

> {"channel": "eb6aaa11-99d0-4f64-9e8c-1140872a423d", "channel_name":
> "ticker.BTCAUD", "op": "private", "origin": "broadcast", "private":
> "ticker", "ticker": {
>     "high": {
>         "value": "121.51941",
>         "value_int": "12151941",
>         "display": "AU$121.51941",
>         "display_short": "AU$121.52",
>         "currency": "AUD"
>     },
>     "low": {
>         "value": "118.00001",
>         "value_int": "11800001",
>         "display": "AU$118.00001",
>         "display_short": "AU$118.00",
>         "currency": "AUD"
>     },
>     "avg": {
>         "value": "119.58084",
>         "value_int": "11958084",
>         "display": "AU$119.58084",
>         "display_short": "AU$119.58",
>         "currency": "AUD"
>     },
>     "vwap": {
>         "value": "119.80280",
>         "value_int": "11980280",
>         "display": "AU$119.80280",
>         "display_short": "AU$119.80",
>         "currency": "AUD"
>     },
>     "vol": {
>         "value": "249.73550646",
>         "value_int": "24973550646",
>         "display": "249.73550646\u00a0BTC",
>         "display_short": "249.74\u00a0BTC",
>         "currency": "BTC"
>     },
>     "last_local": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "last_orig": {
>         "value": "108.99500",
>         "value_int": "10899500",
>         "display": "$108.99500",
>         "display_short": "$109.00",
>         "currency": "USD"
>     },
>     "last_all": {
>         "value": "118.79965",
>         "value_int": "11879965",
>         "display": "AU$118.79965",
>         "display_short": "AU$118.80",
>         "currency": "AUD"
>     },
>     "last": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "buy": {
>         "value": "118.50000",
>         "value_int": "11850000",
>         "display": "AU$118.50000",
>         "display_short": "AU$118.50",
>         "currency": "AUD"
>     },
>     "sell": {
>         "value": "119.99939",
>         "value_int": "11999939",
>         "display": "AU$119.99939",
>         "display_short": "AU$120.00",
>         "currency": "AUD"
>     },
>     "item": "BTC",
>     "now": "1376715241731341" }}

您可以在这里验证:http://jsonlint.com

此外,可能值得一提的是,我已经尝试过解析和删除转义字符。还尝试了几个不同的序列化器,得到了相同的结果

最佳答案

您正在逐 block 获取数据。。 block 本身可能不是完整的 JSON 对象。要么缓冲所有数据,要么使用某些东西来为您执行此操作(例如 request 模块),或者如果您需要解析长流,请查看 JSONparse 模块。

关于json - NodeJS 从流中反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18295549/

相关文章:

c# - 如何使用 json.net 构建用于序列化的对象层次结构?

javascript - 如何在 php 中捕获 JSON 数据并将其发送到我的电子邮件

javascript - 带有 SQLSRV 和 JSON 的 jQuery 数据表

JSON 格式错误

javascript - 从 Node.js 更新客户端 DOM

node.js - 使用变量作为带有修饰符的 mongodb 中的键名

javascript - 具有函数式编程(纯路由)的 Express.js 服务器

c++ - 使用 sf_open_fd() 时 libsndfile 总是出现段错误

java - 当 Java 应用程序以 block 的形式读取巨大文件时减少内存占用

c# - 难以将图像保存到 MemoryStream