[英]Jsoncpp problems
I am using Jsoncpp to parse json-formats for c++. 我正在使用Jsoncpp来解析c ++的json格式。 I do not understand how it works though;
我不明白它是如何工作的; there is a lack of documentation and examples to get me started, and I was wondering if anyone could give me some quick pointers.
开始时缺乏文档和示例,我想知道是否有人能给我一些快速指示。 The only examples I've found deals with files...
我发现的唯一例子涉及文件......
I'm using a HTTP stack to get a json-message in a buffer. 我正在使用HTTP堆栈在缓冲区中获取json消息。 For example, a buffer contains the message
{"state":"Running"}
. 例如,缓冲区包含消息
{"state":"Running"}
。 How do I use the Json::reader to parse this? 我如何使用Json :: reader来解析它? Again the only example I've found deals with reading from files
我发现的唯一例子就是从文件中读取文件
How do you write values to a Json-message? 你如何写一个Json消息的值? For example I want to write
"monkey : no"
and "running : yes"
to a Json-message which I can then use in my GET request. 例如,我想在Json消息中写入
"monkey : no"
和"running : yes"
,然后我可以在我的GET请求中使用它。
Thanks 谢谢
UPDATE: 更新:
on 1), for example, how to parse a buffer containing a json-message like this: 1),例如,如何解析包含json消息的缓冲区,如下所示:
char* buff;
uint32_t buff_size;
Maybe this is good sample for first part of your question: 也许这是你问题第一部分的好样本:
Json::Value values;
Json::Reader reader;
reader.parse(input, values);
Json::Value s = values.get("state","default value");
There is anything but lack of documentation. 除了缺乏文档之外什么都没有。 Yes, it's mainly reference documentation, but it's quite good and well cross-linked.
是的,它主要是参考文档,但它非常好并且交叉链接很好。
Sample code for your reference, below: 示例代码供您参考,如下:
file.json
file.json
{
"B":"b_val2",
"A":{
"AA":"aa_val1",
"AAA" : "aaa_val2",
"AAAA" : "aaaa_val3"
},
"C":"c_val3",
"D":"d_val4"
}
jsoncpp usage scenario as below, for above sample json file. jsoncpp使用场景如下,适用于上面的示例json文件。
#include <iostream>
#include "json/json.h"
#include <fstream>
using namespace std;
int main(){
Json::Value root;
Json::Reader reader;
const Json::Value defValue; //used for default reference
std::ifstream ifile("file.json");
bool isJsonOK = ( ifile != NULL && reader.parse(ifile, root) );
if(isJsonOK){
const Json::Value s = root.get("A",defValue);
if(s.isObject()){
Json::Value s2 = s.get("AAA","");
cout << "s2 : " << s2.asString() << endl;
}else{
cout << "value for key \"A\" is not object type !" << endl;
}
}
else
cout << "json not OK !!" << endl;
return 1;
} }
Output:: 输出::
s2 : aaa_val2
s2:aaa_val2
Additionally, I have used the "amalgamate.py" for generating and using the jsoncpp for the sample source above. 另外,我使用了“amalgamate.py”来生成和使用上面的示例源的jsoncpp。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.