Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize data Reading by JsonCpp

Shulou Source: shulou.com Published: 2022-06-02 11:36:12 09月22日 Update

This article mainly introduces JsonCpp how to achieve data reading, the article is very detailed, has a certain reference value, interested friends must read it!

Jsoncpp is a cross-platform open source library for C++. Download address: http://sourceforge.net/projects/jsoncpp/. Assert as an error handling method, will stop the running of the program, remember!

1) parsing json information from a file

Std::ifstream ifs

Ifs.open ("testdata.json", std::ios::binary)

Json::Reader reader (Json::Features::strictMode ())

Json::Value root

If (NULL = = reader.parse (ifs, root))

{

Ifs.close ()

Return

}

Ifs.close ()

2) parsing json information from cache

Std::string strJsonText = "{\" total\ ": 1,\" toReturn\ ": [{\" createTime\ ":\" 200808061145260000800\ ",\" createUser\ ":\" Zhang San\ "}],\" success\ ": false}"

Json::Reader reader (Json::Features::strictMode ())

Json::Value root

If (NULL = = reader.parse (istrJsonText, root)) return

Note:

1)

When JsonCpp parses illegal json, it will actively tolerate fault into character types, and when the character type is subscript, the assert termination program will be triggered. Solution: enable strict mode, return false when parsing illegal json, and no longer automatically tolerate faults. When creating a read object, specify the property: Json::Reader reader (Json::Features::strictMode ())

Use of Json::Reader reader is not recommended

2) if the text file in Json format is opened with Windows notepad, the BOM logo may be added to the text, resulting in reading failure. It is recommended to use Notepad++ to modify and view it.

The above is all the contents of the article "how to achieve data reading in JsonCpp". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Data information content characters files text methods programs articles types fault tolerance subscript value host interest address object boy partner suggestion Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker MySQL NVidia OPPO Reno