JSON format
[{Key: value, key2: value2}, {key3: value3}]
1. [] contains arrays.
2. {} contains a set.
3. Use: To separate key and Value
4. Separate each data unit by commas. The data unit is divided into arrays and metadata (that is, a key or value)
Example
Simple set
{& Quot; Name & quot;: & quot; Norton & quot;, & quot; age & quot;: 100}
The array can contain a set.
[{"Name": "dark", "Age": 20 },{ "name": "Norton", "Age", 40}]
The set can contain arrays.
[{"Name": "dark", "Age": 20 },{ "name": "Norton", "Age", 40,
[{"Name": "Norton", "Age": 100 },{ "name": "Norton", "Age": 100}]
}]
The value can be an array.
{"Array", [{"name": "dark", "Age": 20 },{ "name": "Norton", "Age", 40}]}
JSON third-party Library:
1. jsoncpp
Http://sourceforge.net/projects/jsoncpp/
Download jsoncpp
How to Use Win32:
1) Add an existing file in the project-> import the include and SRC files.
2) if someone else's project is introduced, configure the header file and add the file again. Otherwise, the file may not be found.
2. libjson-there are multiple language versions, C ++, Java, C # and so on (JSON is only a naming rule, so it can be processed in multiple languages)
Http://sourceforge.net/projects/libjson/ libjson downloading
This library is also very common. The configuration is as follows:
1) decompress the file and add jsonoptions. h and libjson. h.
2) then add all the files under D: \ cocos2d-x \ tools \ libjson_7.6.1 \ libjson \ _ internal \ source to the project. You can create a folder libjson in the preceding two steps.
3) Note that libjson has two libraries: C/C ++, which must be specified during compilation. In Win32, the default mode is release rather than debug. Therefore, an error is reported in debug mode.
4) as described in 3, you need to find the header file jsonoptions. h In the loaded header file.
Search for # define json_debug and keep it open (that is, do not comment on this line). However, if you want to publish and perform release compilation, You Need To comment out this line.
Search for # define json_library. If you want to compile it in C ++, comment the line. Because c ++ is used in the cocos2d-x, the line needs to be commented out (not commented by default)
The configuration is complete.
Jsoncpp instance
void HelloWorld::readJsonCpp(){unsigned long size; char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson.json","r", &size);Json::Reader reader;Json::Value root;if (!reader.parse(std::string(file),root, false)){return ;}std::string name = root["name"].asString();int age = root["age"].asInt();CCLOG("name=%s",name.c_str());CCLOG("age=%d",age);////////////////////////////////////file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson2.json","r", &size);if (!reader.parse(std::string(file),root, false)){return ;}size = root.size();for (int i=0; i<size; ++i){name = root[i]["name"].asString();age = root[i]["age"].asInt();CCLOG("name=%s,age=%d",name.c_str(),age);}}void HelloWorld::writeJsonCpp(){Json::Value root;Json::FastWriter writer;Json::Value person;person["name"] = "hello world";person["age"] = 100;root.append(person);std::string json_file = writer.write(root);char filePath[1024]= {'\0'};memset(filePath,0,sizeof(filePath));strcat(filePath,CCFileUtils::sharedFileUtils()->getWriteablePath().c_str());strcat(filePath,"writeJsonCpp.json");FILE* file = fopen(filePath,"w+");fwrite(json_file.c_str(),json_file.size(),1,file);fclose(file);}
Libjson instance
The content of testlibjson. JSON is as follows:
{"RootA" : "Value in parent node","ChildNode" : [{"ChildA" : "String Value c1","ChildB" : "dsf c1"},{"ChildA" : "String Value c2","ChildB" : "dsf c2"}]}
Void helloworld: readlibjson () {// read jsonunsigned long size; char * STR = (char *) ccfileutils: sharedfileutils ()-> getfiledata ("testlibjson. JSON "," r ", & size); If (libjson: is_valid (STR) = false) {Delete STR; STR = NULL; printf (" parse faild! \ N "); System (" pause "); exit (0);} jsonnode Rn = libjson: parse (STR); Delete STR; STR = NULL; int TMP = rn. size (); cclog ("% d", TMP); For (INT I = 0; I <rn [1]. size (); I ++) {jsonnode temp = rn [1] [I]; for (Int J = 0; j <temp. size (); j ++) {cclog ("% s: % s", temp [J]. name (). c_str (), temp [J]. as_string (). c_str () ;}} void helloworld: writelibjson () {// write jsonjsonnode N (json_node); N. push_back (jsonnode ("roota", "value in parent node"); jsonnode C (json_array); C. set_name ("childnode"); jsonnode C1 (json_node), C2 (json_node); c1.push _ back (jsonnode ("childa", "string value C1 ")); c1.push _ back (jsonnode ("childb", "DSF C1"); c2.push _ back (jsonnode ("childa", "string value C2 ")); c2.push _ back (jsonnode ("childb", "DSF C2"); C. push_back (C1); C. push_back (C2); N. push_back (c); cclog ("= % s", N. write_formatted (). c_str (); // write_farmatted can get N content unsigned long size; char filepath [1024] = {'\ 0'}; memset (filepath, 0, sizeof (filepath); strcat (filepath, ccfileutils: sharedfileutils ()-> getwriteablepath (). c_str (); strcat (filepath, "testlibjson. JSON "); file * file = fopen (filepath," W + "); fwrite (N. write_formatted (). c_str (), N. write_formatted (). size (), 1, file); // write_farmtted can get the file content size fclose (File );}