C + + JSON string conversion to map management

Source: Internet
Author: User

In COCOS2DX for Lua, we often use LUA's table to pass in C + + and then manipulate the data early in the C + + layer.

The implementation steps are roughly as follows:

Table->string->c++ layer, managed by Rapidjson resolution, stored in map

In Lua, the conversion table is roughly the following

Local TBL = {}

Tbl["Fang"] = 1

tbl["jian"] = 1.4

Tbl["Heng"] = True

tbl["FJH"] = 12345677

tbl["what"] = "fuck"

Local str = Json.encode (TBL)

--Print Str:

{"Fang": 1, "Jian": 1.4, "Heng": True, "Fjh": 12345677, "what": "Fuck"}

The STR is passed into the C + + layer and parsed by Rapidjson::D Ocument, however, the data type in the JSON string is indeterminate,

But touch has encapsulated a Cclvalue class for us to use, it can store any value simple data type to save, and then need to

Time to get it back. CCLValue.h and cclvalue Two file downloads are available here:

Http://pan.baidu.com/s/1c06Z0xM

The following methods are used to convert a string into a map-saved method:

Map<string,lvalue> testlvalue::jsonstringtomap (const string& jsonstring)

{

Rapidjson::D ocument doc;

Doc. SetObject ();

Doc. Parse<rapidjson::kparsedefaultflags> (Jsonstring.c_str ());

Map<string,lvalue> m;

for (Rapidjson::value::memberiterator iter = doc. Memberonbegin (); iter! = Doc. Memberonend (); iter++)

{

const char * key = Iter->name. GetString ();

Const rapidjson::value& val = iter->value;

if (Val. Isdouble ())

M.insert (Make_pair (Key, LValue (Val. GetDouble ())));

else if (Val. Isbool ())

M.insert (Make_pair (Key, LValue (Val. Getbool ())));

else if (Val. Isint ())

M.insert (Make_pair (Key, LValue (Val. GetInt ())));

else if (Val. IsInt64 ())

M.insert (Make_pair (Key, LValue (Val. GetInt64 ())));

else if (Val. Isstring ())

M.insert (Make_pair (Key, LValue (Val. GetString ())));

}

return m;

}

The procedure is simple, the value of the LValue in the map<string,lvalue> of the return value needs to be judged by its data type and then obtained

if (It->second.gettype () ==lvalue::type::D ouble) {

Double value = it->second.asdouble ();

}

else if (It->second.gettype () ==lvalue::type::integer) {

int value = It->second.asint ();

}

.

.

.

and float, string, int64, bool, int64 don't write it down.

Reprint please indicate the source, from the blog Park Hemjohn

C + + JSON string conversion to map management

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.