简体   繁体   中英

how can I receive Newline delimited JSON in my client?

I can't find any way to deal with that topic. Basically I'm receiving from my API a stream in ndJson format. Just found a npm module to manage ndjson in server side. How can I set up a client play with Newline delimited JSON. Any suggestion will be helpfull. Thanks

You divide you input up on new lines. How you do this depends on how you are getting the input.

You might have it all arrive as one big string, in which case you can str.split("\\n") it.

You might be getting it coming on continuously, in which case you'll probably be grabbing a number of bytes each time and will want to split each chunk as above, but prepend the last portion of it to the new chunk (to handle a line being split over two chunks of data).

Once you have your input divided up, run each JSON text (which will be each member of the array if you use split) through JSON.parse() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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