Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

json parsing


May 21, 2021 CrossApp



CrossApp uses lib_json to parse the johnson file. lib_json we've joined LibExtensions, and we're very handy to use on CrossApp.


Start by introducing the header file

#include "CrossAppExt.h"
using namespace CSJson;


Let's familiarize ourselves with a few class names and functions

   /*
    Value:写过脚本、弱语言的童鞋应该很清楚var,其他Value 和var一个道理,都是可以表示很多数据类型的数据类型。 
    这话可能比较绕,简单说就是Value你可以理解可以是int 也可以是string 也可以是其他数据类型。
    当然定义 Value value,只是个定义,还没有决定其数据类型,如果你Value value =10;那么value 就是个整型
    在用于JSON时,我们常表示为一个map,其中包括 key-value,键值对
    其中Value 中包括一些将其转为基础数据类型的6个方法,如下:
        value.asCString();
        value.asString();
        value.asBool();
        value.asDouble();
        value.asInt();
        value.asUInt();
    */
         
    Value map;
     
    /*
    FastWriter:起作用是将Value数据编码成JSON格式的数据
    常用函数:write(<#const Json::Value &root#>)
    */
    Json::FastWriter write;
         
    /*
    Value:作用与FastWriter相反,是将JSON格式的数据解析成一个Value
    常用函数: reader.parse(<#std::istream &is#>, <#Json::Value &root#>)
    */
    Json::Reader reader;

From the comments above, we can clearly understand the meaning of functions. Let's start generating The Johnson data and parsing the Johnson data.


Json data generation

//先定义数据
    Value map;
    map["name"] = "9miao";
    map["password"] = "123456";
    map["Email"] = "[email protected]";
    map["PHONE"] = 10086;
     
    //编码成json数据
    FastWriter  write;
    string jsonData = write.write(map);
     
    //打印结果
    CCLog("jsonData:%s", jsonData.c_str());

Print results above:

jsonData:{"Email":"[email protected]","PHONE":10086,"name":"9miao","password":"123456"}

Json data parsing

Sometimes we need to parse the jason files in the Resources directory, and first we need to copy the jason files into the Resources directory. We copied the following Johnson format file into the Resources directory under the name info.json.

{    "info":     [        {"name":"aaa","num":"0001"},        {"name":"bbb","num":"0002"},        {"name":"ccc","num":"0003"},        {"name":"ddd","num":"0004"},        {"name":"eee","num":"0005"},        {"name":"fff","num":"0006"},        {"name":"ggg","num":"0007"},        {"name":"hhh","num":"0008"},        {"name":"iii","num":"0009"},        {"name":"jjj","num":"0010"},        {"name":"kkk","num":"0011"},        {"name":"lll","num":"0012"},        {"name":"mmm","num":"0013"},        {"name":"nnn","num":"0014"},        {"name":"ooo","num":"0015"},        {"name":"ppp","num":"0016"}    ],         "gender": "male",    "occupation": "coder"}


We add the following code to the program to resolve it:

Reader reader;
    //定义Valuer
    Value value;
     
    //json文件路径
    string jsonFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("info.json");
     
    //将文件生成CCString对象
    CCString* json = CCString::createWithContentsOfFile(jsonFile.c_str());
     
    //将数据解析到value中
    if (reader.parse(json->getCString(),value))
    {
        int length = value["info"].size();
         
        //循环解析子节点
        for (int index = 0; index < length; index++)
        {
            std::string name = value["info"][index]["name"].asString();
            std::string num = value["info"][index]["num"].asString();
             
            CCLog("name:%s", name.c_str());
            CCLog("num:%s", num.c_str());
        }
         
        //获取方式一
        Value valueGender;
        valueGender = value.get("gender", valueGender);
        std::string gender = valueGender.asCString();
         
        //获取方式二
        std::string occupation = value["occupation"].asCString();
        CCLog("gender:%s", gender.c_str());
        CCLog("occupation:%s", occupation.c_str());
    }