这是 Sven(NeoJSON 的作者)在 pharo-users 邮件列表中的回答(他不在 SO 上):
阅读“格式”很容易,只需继续为每个 JSON 表达式执行#next(忽略空格)。
| data reader |
data := '{"smalltalk": "cool"}
{"pharo": "cooler"}'.
reader := NeoJSONReader on: data readStream.
Array streamContents: [ :out |
[ reader atEnd ] whileFalse: [ out nextPut: reader next ] ].
防止中间数据结构也很容易,使用流。
| client reader data networkStream |
(client := ZnClient new)
streaming: true;
url: 'https://github.com/NYPL-publicdomain/data-and-utilities/blob/master/items/pd_items_1.ndjson?raw=true';
get.
networkStream := ZnCharacterReadStream on: client contents.
reader := NeoJSONReader on: networkStream.
data := Array streamContents: [ :out |
[ reader atEnd ] whileFalse: [ out nextPut: reader next ] ].
client close.
data.
花了几秒钟,毕竟 50K 项目在网络上是 80MB+。