锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / jsoncpp入门技巧
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

jsoncpp入门技巧


Jsoncpp入门经验

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格式。我不明白它是如何工作的; 缺少文档和示例让我入门,我想知道是否有人能给我一些快速指示。我发现的唯一例子是处理文件的......

  1. I'm using a HTTP stack to get a json-message in a buffer. For example, a buffer contains the message {"state":"Running"}. How do I use the Json::reader to parse this? Again the only example I've found deals with reading from files我正在使用HTTP堆栈在缓冲区中获取json消息。例如,缓冲区包含该消息{"state":"Running"}。我如何使用Json :: reader来解析它?我发现的唯一例子就是从文件中读取并解析
  2. How do you write values to a Json-message? 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"成JSON的消息,进而我可以再在我的GET请求使用消息。

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. 除了缺乏文档之外什么都没有。是的,它主要是参考文档,但它非常好并且交叉链接很好

  1. Just read the documentation只需阅读文档
  2. Just use this class or possibly use the other class
  3. 只需使用此类或可能使用其他类

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

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内