简体   繁体   English

如何读取json格式文本的字符流

[英]how to read a character stream of a json formatted text

I am dealing with a sub-process (created using fork/exec -- aka the child ) that outputs on its std-out some json formatted text. 我正在处理一个子过程(使用fork / exec创建-aka the child ), the child在其std-out上输出一些json格式的文本。

I have a pipe between the parent and child process, where the parent is reading the child std-out a character at a time (using getc ). 我在父进程和子进程之间有一个管道,其中父进程一次读取子标准输出一个字符(使用getc )。

The parent is not using the blocking waitpid function but keeps checking the io pipe for any characters in it. 父级不使用阻塞的waitpid函数,而是继续检查io管道中是否包含任何字符。

-- Up to here everything is implemented and functional -- -至此一切都已实现并发挥作用-

I am using a json c++ library that can be used to read json files. 我正在使用可用于读取json文件的json c ++库。

But obviously a half written json file can't be read; 但是显然一半的json文件无法读取。 And since the parent is reading from the child a character at a time, how can it keep track of the characters until the first valid json object is detected and so on? 而且由于父级一次从子级读取一个字符,因此它如何跟踪这些字符,直到检测到第一个有效的json对象,依此类推?

Please let me know if I am not clear so I try to rephrase. 如果不清楚,请告诉我,所以我尝试重新措辞。

If your parser does not support streaming, consider another library. 如果您的解析器不支持流式传输,请考虑使用另一个库。

yajl is an ANSI-C parser for streaming JSON, it should work with your C++ code. yajl是用于流JSON的ANSI-C解析器,它应与您的C ++代码一起使用。

There are a bunch of other C and C++ tools listed at the bottom of the json.org homepage. json.org主页底部列出了许多其他C和C ++工具。 One of them might work even better. 其中之一可能会更好。

"First valid json object" is troubling. “第一个有效的json对象”令人担忧。 JSON only permits one top-level object, so you should be able to just read until EOF (assuming you've closed your pipes properly). JSON仅允许一个顶级对象,因此您应该能够读取直到EOF(假设您已正确关闭管道)。

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

相关问题 如何从字符中读取ASCII值并将其转换为十六进制格式的字符串 - How to read ASCII value from a character and convert it into hexadecimal formatted string 使用迭代器读取格式化的流是一件好事吗? - is it a good thing to use iterators to read on a formatted stream? 如何在C ++中读取具有4个字符的文本文件? - How to read text file in C++ with 4 character? 如何在C ++中从文本文件中读取字符 - How to Read from a Text File, Character by Character in C++ 如何将QAbstractItemModel编写为格式化文本? - How to write QAbstractItemModel as formatted text? 如何从文本文件中读取特殊格式的坐标来计算它们之间的距离? - how to read specially formatted coordinates from a text file to calculate distance between them? C ++:如何从格式化的文本文件中读取大量数据到程序中? - C++: How to read a lot of data from formatted text files into program? 如何从文本文件中逐字符读取字符,并将其放入字符数组中? - How do I read character by character from a text file and put it in a character array? 将从格式化文本文件读取的文本数据存储到链表 - Store text data read from a formatted text file to a linked list 如何从文本文件一次读取一个字符 - How to read one character at a time from text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM