精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
Jsoncpp的压缩包解开后没有Sample目录,虽然工程里有个测试的项目,里面有读写代码,但是写的很难理解,main函数里面都是宏,让初始化看宏确实难为人.比如这样: JSONTEST_REGISTER_FIXTURE(runner, ValueTest, StaticString);
这让有2-3年经验的朋友看,大概能理解成测试静态字符串.
国内朋友入门jsoncpp可以找中文页面, 我最早找的是DataToJson函数,后来又找到些数组方面的知识点,基本满足了开发需要.
I am using Jsoncpp to parse json-formats for c++. 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... 我正在使用Jsoncpp来解析c ++的json格式。我不明白它是如何工作的; 缺少文档和示例让我入门,我想知道是否有人能给我一些快速指示。我发现的唯一例子是处理文件的......
UPDATE:补充
1), for example, how to parse a buffer containing a json-message like this: 例如,如何解析包含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");
yeah that's what I'm looking for; I'm not sure how to use reader.parse() though on a buffer; updated OP –是的,这就是我在寻找的东西; 我不知道如何在缓冲区上使用reader.parse()
I think there is no problem to use a buffer instead of a string, but if you have something like char buffer[max] you can use parse (buffer,buffer+max-1, values) 我认为使用缓冲区而不是字符串没有问题,但如果你有类似的东西char buffer[max]可以使用parse (buffer,buffer+max-1, values)
There is anything but lack of documentation. Yes, it's mainly reference documentation, but it's quite good and well cross-linked. 除了缺乏文档之外什么都没有。是的,它主要是参考文档,但它非常好并且交叉链接很好
I have, and I don't fully understand it, which is why I am making this topic我有,而且我不完全理解来去解决此话题
Than you'll have to explain what you don't understand on it. The Json::Reader::parsemethod has 3 overloads. One for input stream (which may be a stringstream), one is for string represented as pointer to beginning and pointer to terminating NUL and one for std::string. The writer classes have simple write method that returns a std::string, or you can just write the Value to an ostream (including stringstream) using standard operator <<. You however have to build the Value, which is also quite well documented. The main page even has a sample你必须解释一下你不明白的东西。该Json::Reader::parse方法有3个重载。一个用于输入流(可以是字符串流),一个用于表示为开始的指针的字符串和用于终止NUL的指针和用于std :: string的指针。writer类具有write返回a的简单方法std::string,或者您可以使用标准运算符将Value写入ostream(包括stringstream)<<。但是,您必须构建值,这也是很好的文档。主页也有一个样本.
I have to agree with KaiserJohaan, this json library's documentation is very lacking. It seems to work well once you figure things out, but it was difficult to learn how to use it我不得不同意KaiserJohaan,这个json库的文档非常缺乏。一旦你解决了问题,它似乎运作良好,但很难学会如何使用它
Sample code for your reference, below: 示例代码供您参考,如下
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
Additionally, I have used the "amalgamate.py" for generating and using the jsoncpp for the sample source above.
另外,我使用了“amalgamate.py”来生成和使用上面的示例源的jsoncpp