zoukankan      html  css  js  c++  java
  • C++ Json工具--Jsoncpp用法简介

    Json简介

    JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式,可读性强,易扩展,很适合做通信协议,下面主要介绍一款C++的Json库:Jsoncpp.

    用法简介

    数据类型

    Jsoncpp中,封装类Json::Value,既可以用于表示Json对象,也可以用于表示Json数组(也叫列表或者集合)。使用起来比较方便;当作数组使用时,在实例化时需要指定其类型如下:

    Json::Value array(Json::arrayValue);
    

    这样就创建了一个空的json数组,数组的成员可以是单个对象,也可以是复杂对象,如下:
    简单成员:

    [ "1", "2", "3" ]   //字符串列表
    [ 1, 2, 3 ]         //整数列表
    

    也可以是:

    	"test":[
    		{
    			"no":1,
    			"val": 21
    		},
    		{
    			"no":2,
    			"val": 21
    		},
    		{
    			"no":3,
    			"val": 32,
    			"msg": "for test"   //列表中每个对象的内容可以不一样
    		}
    	] 
    

    解析如下:
    数组
    对象很简单,就是key-value的形式;value的数据类型可以是整型、浮点、字符串。

    {
    	“no”:1,
    	"name": "Tom"
    }
    

    C++代码示例

    #include <iostream>
    #include "json/json.h"
    #include <string>
    
    
    using namespace std;
    
    int main()
    {
        //1.创建json字符串
        cout << "1.creat json string----------------------------------------"<<endl;
        Json::Value jsonObj;
    
        jsonObj["id"]   = 1;     //int
        jsonObj["name"] = "Fens";//string
        jsonObj["age"]  = 18;    //int
    
        Json::Value jsonArray(Json::arrayValue); //creat an array
        jsonArray[0] = "13633838481"; //给数组添加数据,注:这两个手机号是上学时用的,现在已经不是我的了^_^
        jsonArray[1] = "15617051150";
        jsonObj["phone"]=jsonArray;     //将数组添加到对象中
    
        Json::StyledWriter styleWriter; //有个格式
        Json::FastWriter fastWriter;    //压缩,无格式
        cout << "styleWriter json string: "<<styleWriter.write(jsonObj)<<endl;
        cout << "-----------------------------------------------------------"<<endl;
        cout << "fastWriter json string: "<<fastWriter.write(jsonObj)<<endl;
    
        //2.解析json字符串
        cout << "2.decode json string----------------------------------------"<<endl;
        const string jsonString = "{"id":1,"name":"fens","phone":["15617051150","13833838481"],"school":[{"type":"primary","name":"夏邑县第一实验小学","address":"河南省夏邑县文化路"},{"type":"middle","name":"夏邑县第三高级中学","address":"河南省夏邑县建设路孔祖大道"},{"type":"college","name":"郑州大学","address":"河南省郑州市高新技术开发区科学大道"}]}";
    
        Json::Reader reader;
        Json::Value  rootObj;
        reader.parse(jsonString, rootObj);
    
        cout <<"read json string: "<<rootObj.toStyledString()<<endl;
    
    
    
        return 0;
    }
    
    

    上面代码是介绍的Json字符串的解析与生产,如果需要读写文件,只需要把读取到的字符串交给Json::Reader,或者把Json::styleWriter或者Json::fastWriter序列化后的字符串写入文件即可。

    代码执行输出结果

    1.creat json string----------------------------------------
    styleWriter json string: {
       "age" : 18,
       "id" : 1,
       "name" : "Fens",
       "phone" : [ "13633838481", "15617051150" ]
    }
    
    -----------------------------------------------------------
    fastWriter json string: {"age":18,"id":1,"name":"Fens","phone":["13633838481","15617051150"]}
    
    2.decode json string----------------------------------------
    read json string: {
       "id" : 1,
       "name" : "fens",
       "phone" : [ "15617051150", "13833838481" ],
       "school" : [
          {
             "address" : "河南省夏邑县文化路",
             "name" : "夏邑县第一实验小学",
             "type" : "primary"
          },
          {
             "address" : "河南省夏邑县建设路孔祖大道",
             "name" : "夏邑县第三高级中学",
             "type" : "middle"
          },
          {
             "address" : "河南省郑州市高新技术开发区",
             "name" : "郑州的大学",
             "type" : "college"
          }
       ]
    }
    
    

    JSON在线解析及格式化验证 - JSON.cn

    Json在线解析网站: https://www.json.cn/#

    Jsoncpp源码及示例代码: https://gitee.com/fenstec/demo_code.git


  • 相关阅读:
    1009 Product of Polynomials (25分)
    VS code 调试C++
    1065 A+B and C (64bit) (20分)
    UML与数据库应用系统
    语句(switch,异常,NDEBUG,assert)
    1046 Shortest Distance (20分)
    1042 Shuffling Machine (20分)
    模块和包
    闭包&装饰器
    迭代器、生成器
  • 原文地址:https://www.cnblogs.com/fensnote/p/13436460.html
Copyright © 2011-2022 走看看