https://blog.csdn.net/u013456468/article/details/124908853
https://blog.csdn.net/centnetHY/article/details/83316090
https://blog.csdn.net/zhanglehes/article/details/128147992

http://www.rosoo.net/a/202303/17869.html (demo)

  • json解析

    list<BoardInfo> result;
    Json::Reader r;
    Json::Value root;
    if (r.parse(json, root)) {
        int result_code = root["code"].asInt();
        if (result_code == 0) {
            Json::Value result_data = root["data"];
            //数组的读取
            Json::Value boardList = result_data["list"];
            int size = boardList.size();
            for (int i(0); i < size; i++) {
                BoardInfo boardInfo;
                Json::Value boardListItem = boardList[i];
                boardInfo.sn = boardListItem["sn"].asString();
                boardInfo.ip = boardListItem["ip"].asString();
                boardInfo.deviceStatus = boardListItem["deviceStatus"].asInt();
                result.push_back(boardInfo);
            }
        } else {
            LOG(ERROR) << "code error :" << result_code;
        }
    } else {
        LOG(ERROR) << "parse json error";
    }
  • 实体类转json

Json::Value root;
        Json::Value arrayObj;
        Json::Value item;

        root["memo"] = pStructData->szMemo;
        root["file_count"] = pStructData->nCount;
        // 生成file_list数组
        for(int i = 0; i < pStructData->nCount; i ++)
        {
            item["num"] = pStructData->szFileList[i].nNum;
            item["file"] = pStructData->szFileList[i].szFile;
            arrayObj.append(item);
        }
        root["file_list"] = arrayObj;
        // JSON转换为JSON字符串(已格式化)
        std::string strOut = root.toStyledString();
        //  JSON转换为JSON字符串(未格式化)
        //Json::FastWriter writer;
        //std::string strOut = writer.write(root);

Android平台版本的jsoncpp下载及编译

https://www.jianshu.com/p/a882913c604d
github地址:https://github.com/allanmax/a_jsoncpp_android

  • android平台的一些问题
    1)asstring方法无法使用,要用asCstring。
分类: json解析

0 条评论

发表回复

您的电子邮箱地址不会被公开。