浅谈整理C++关于Json的解析

一、Json简述  

     1. JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。JSON是一个标记符的序列。这套标记符包含六个构造字符字符串数字和三个字面名

二、Json构成

2.可以是对象数组数字字符串或者三个字面值(false、null、true)中的一个。值中的字面值中的英文必须使用小写。

2.1对象由花括号括起来的逗号分割的成员构成,成员是字符串键和上文所述的由逗号分割的键值对组成,如:

1

    { "name""John Doe""age": 18, "address": { "country" "china""zip-code""10000"}}

2.2数组是由方括号括起来的一组值构成,如:

1

[3, 1, 4, 1, 5, 9, 2, 6]

2.3 字符串与C或者Java的字符串非常相似。字符串是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

2.4数字也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

三、Json的获取

JsonCpp是一个开源库

下载地址:https://github.com/open-source-parsers/jsoncpp

官方文档:https://github.com/open-source-parsers/jsoncpp/wiki/Amalgamated

四、Json的解析方式

第一种是jsoncpp也就是以上那种,第二种是rapidjson库,第一种我们常用的一种,第二种相对来说简单,只需要引入头文件就可以。

五、Json解析实例

1.读取json字符串

#include <iostream>
#include <string>
#include <json/json.h>
 
using namespace std;
 
int main()
{
    string strJsonInfo = "{\"role_id\": 1,\"person_name\": \"关羽\",\"person_hobby\": \"练武\"}";
 
    int nRoleId = 0;
    string strperson_name = "";
    string strperson_hobby = "";
    
    Json::Reader reader;
    Json::Value root;
 
    if (reader.parse(strJsonInfo, root))
    {
        nRoleId = root["role_id"].asInt();
        strperson_name = root["person_name"].asString();
        strperson_hobby = root["person_hobby"].asString();
    }
 
    cout << "role_id is: " << nRoleId << endl;
    cout << "person_name is: " << strperson_name << endl;
    cout << "person_hobby is: " << strperson_hobby << endl;
 
    return 0;

}

2.写入json字符串

#include <iostream>
#include <string>
#include <json/json.h>
 
using namespace std;
 
int main()
{
    string strJsonInfo = " ";

    int nRoleId = 1;
    string str_student_name = "黎明";
    string str_student_hobby = "唱歌";

    Json::StyledWriter writer;
    Json::Value root;

    root["nRoleId"] = nRoleId;
    root["student_name"] = str_student_name;
    root["student_hobby"] = str_student_hobby;

    string str_JsonData = writer.write(root);
 
    cout << "JsonData is: " << str_JsonData << endl;

 
    return 0;

}

结果:

{
   "nRoleId" : 1,
   "student_hobby" : "唱歌",
   "student_name" : "黎明"
}

第二种

C++ JSON解析库RapidJSON

https://github.com/Tencent/rapidjson

不说了看实例:

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

int main() {
    // 1. Parse a JSON string into DOM.
    const char* json = "{\"project\":\"学步园\",\"stars\":10}";
    Document d;
    d.Parse(json);

    // 2. Modify it by DOM.
    Value& s = d["stars"];
    s.SetInt(s.GetInt() + 1);

    // 3. Stringify the DOM
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    // Output {"project":"学步园","stars":10}
    std::cout << buffer.GetString() << std::endl;
    return 0;
}

这个方法就不过多介绍了,引用一网友例子这个方法,看起来不错有兴趣看看:

https://blog.csdn.net/qq_27385759/article/details/79277434

猜你喜欢

转载自blog.csdn.net/leiyang2014/article/details/103110091