C++:使用libjsoncpp读写json文件

"libjsoncpp"

json不支持8进制和16进制数值转换。

#include "json/json.h"

    Json::Reader reader;
    Json::Value root;

    std::ifstream in("/data/shared/dtc_info.json", std::ios::binary);

    if(!in.is_open())
    {
        ALOGE("[%s] Error opening file", __func__);
        return;
    }

    if(reader.parse(in, root))
    {
        int node_size = root["DTC Information"].size();

        ALOGI("[%s] node_size: %d", __func__, node_size);

        for(auto const & dtc_node : root["DTC Information"])
        {
            std::string dtc_code = dtc_node["DTC Code"].asString();
            std::string dtc_info = dtc_node["DTC Context"].asString();

            uint32_t dtc_value = static_cast<uint32_t>(std::stoul(dtc_code.c_str(), nullptr, 16));

            ALOGI("[%s] dtc code: %x", __func__, dtc_value);
            ALOGI("[%s] dtc info: %s", __func__, dtc_info.c_str());
        }

        in.close();
    }
    else
    {
        ALOGE("[%s] reader parse file failed", __func__);
    }
{
	"DTC Information" : [
	    {
			"DTC Code" : "0x11111111",
			"DTC Context" : "..."
		},
		{
			"DTC Code" : "0x22222222",
			"DTC Context" : "..."
		}
	]
}

猜你喜欢

转载自blog.csdn.net/xikangsoon/article/details/109510607