The previous article explains how to use the initiate request and accept data, and this article begins using Libjson to parse network JSON data .
COCOS2DX itself does not have a JSON parsing class library, we introduce libjson here to parse. Download Address: http://sourceforge.net/projects/libjson/files/
The version I am currently using is 7.6.1. 1. Download Libjson and unzip the download, extract out, here to our useful documents are: libjson.h; JSONOptions.h; All files under the _internal->source folder; All files under the _internal->dependencies folder. Copy these files and folders and place them in the classes directory in your project, noting that the file hierarchy cannot be changed . 2. Add source to project for clearer project structure, create a new folder for JSON source.
Then add the head culture and CPP files in the Libjson. The following figure is the file I added; 3. For JSON parsing , first introduce Libjson into your code:
#include "Libjson.h"
Then you can write the JSON parsing class. If not, you can refer to the documents in the->getting started->c++ interface under the downloaded Libjson folder.
Jsonnode N (json_node);
N.push_back (Jsonnode ("Roota", "Value in parent Node"));
Jsonnode c (json_node);
C.set_name ("Childnode");
C.push_back (Jsonnode ("Childa", "String Value"));
C.push_back (Jsonnode ("Childb");
N.push_back (c);
std::string JC = n.write_formatted ();
Std::cout << JC << Std::endl;
OK, and then run and view the output.