Under Cocos2dx3.0, The JSON Parsing Library has been officially integrated, so we can reference it.
JSON file hello. json content
{"pets":["dog","cat"],"stuInfo":{"stuAge":"23","stuName":"zhangsan","birthday":"1990-01-12"},"username":"tomsfff","other":[true,30]}
. H header file
#include "cocos-ext.h"#include "json/document.h"
. Cpp
// Obtain the file path const char * file_path = FileUtils: getInstance ()-> fullPathForFilename ("hello. json "). c_str (); log ("external file path = % s", file_path); rapidjson: Document d1; std: string contentStr = FileUtils: getInstance () -> getStringFromFile (file_path); d1.Parse <0> (contentStr. c_str (); // print the content of the JSon file printf ("% s \ n", contentStr. c_str (); // obtain the const rapidjson: Value & v = d1 ["pets"]; if (v. isArray () {// The variable I must be an unsigned int. Otherwise, an error is returned for (unsigned int I = 0; I <v. size (); ++ I) {const rapidjson: Value & val = v [I]; log ("% s", val. getString () ;}} else {const rapidjson: Value & val = v ["stuAge"]; log ("val. getString () = % s ", val. getString ());}