JSON parsing function using CEF

Source: Internet
Author: User

CEF provides JSON parsing capabilities, and there are three JSON-related methods within the Cef_parser.h file:

    • Cefparsejson
    • Cefparsejsonandreturnerror
    • Cefwritejson

Use the simplest Cefparsejson method to make a small example to illustrate the usage. The function prototypes are as follows:

CefRefPtr<CefValue> CefParseJSON(const CefString& json_string,                             cef_json_parser_options_t options);

The first parameter is a JSON string with parsing, the type is cefstring, the actual use of char* or std::string can be passed, cefstring can be automatically constructed according to them.

The second is an enum-type option (defined within CEF_TYPES.H), with a value of JSON_PARSER_RFC and Json_parser_allow_trailing_commas two, The following enumeration value means allowing the JSON string to end with a comma (a comma-terminated JSON character may be considered non-compliant by the standard parser).

The return value is Cefvalue. Cefvalue is a generic type, defined in Cef_values.h, that can represent types of booleans, integers, double-precision floating-point numbers, strings, binaries, dictionaries, and lists (enumeration type cef_value_type_t is defined in cef_types.h).

The Cefvalue GetType () method can return the actual data type. Then there are Getbool, GetString, GetInt, Getdictionary, and so on to return certain types of data.

Well, with these backgrounds, you can parse the JSON string.

The sample JSON string (a simple dictionary) is as follows:

{    "result":0,    "token":"abc-k-xxx-poi",    "id":821251852}

Parse the code snippet as follows:

...CefRefPtr<CefValue> jsonObject = CefParseJSON(strJoinKey, JSON_PARSER_ALLOW_TRAILING_COMMAS);if (jsonObject->IsValid()){    CefRefPtr<CefDictionaryValue> dict = jsonObject->GetDictionary();    CefString token = dict->GetString("token");    int id = dict->GetInt("id");    int result = dict->GetInt("result");}

That's it.

Other reference articles are described in my column: "CEF and Ppapi development ".

JSON parsing function using CEF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.