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