Method of json data parsing: first create a JSON file; then include the "document.h" and "cocos-ext.h" header files in the class; then obtain the JSON file path through FileUtils and pass the Document object Parse JSON data; finally obtain different types of data values.
# Data needs to be transmitted during network communication. The process of JOSN data parsing is: first create a JSON file, and then include document.h and cocos- in the class. ext.h header file, then obtain the JSON file path through FileUtils, parse the JSON data through the Document object, and finally obtain different types of data values.
JSON (JavaScript Object Notation) is a lightweight data exchange format. It makes it easy for people to read and write, and it also facilitates machines to parse and generate. JSON uses a text format that is completely independent of programming languages, but also uses C-like language habits (including C, C, C#, Java, JavaScript, Perl and Python, etc.). These characteristics make JSON an ideal data exchange language.
JSON data parsing
For example, under the external/json directory in the Cocos2d.x root directory, there are classes related to JSON processing. The document.h header is mainly used here. file. The two core classes in this file are GenericValue and GenericDocument. GenericDocument inherits GeneficValue. GenericDocument is used to process document content, such as parsing document content; GenericValue mainly processes value content, that is, the key-value pair content inside the document, and the value can be obtained based on the key. Both GenericValue and GenericDocument have been retyped. So you can use the name after the type definition.
ypedef GenericDocument
typedef GenericValue
Value overloads the array subscript operator [ ], so we can use this operator to get the value based on the key in the JSON file.
const GenericValue & operator [] (const Ch* name) const{
const_cast
Value also provides a Group GetXXX method to obtain corresponding values according to different data types.
An example is used to demonstrate how to parse JSON data. The steps are as follows:
Create a JSON file
In the project Create a JSON file under the classes folder with the following content:
{"pets":["dog","cat"],"stuInfo":{ "stuAge":23,"stuName":"rose","birthday":"1990-01-12"},"username","tom","other":[true,30]}
In this file, pets is an array, representing pets, with two values dog and pet; stuInfo is a student information; followed by a usename ; Finally there is an other array.
Include document.h and cocos-ext.h header files in the class
#include "cocos-ext.h" #include "json/document.h"
Get the JSON file path through FileUtils
const char* file_path = FileUtils::getInstance()->fullPathForFilename("hello.json").c_str(); log("文件路径path=%s",file_path);
Parsing JOSN data through Document objects
//文档对象 rapidjson::Document dl; //获得JSON字符串内容 std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path); //解析 dl.Parse<0>(contentStr.c_str()); //输出JSON文件的内容 printf("%s\n",contentStr.c_str());
Obtain different types of data values
Use the array subscript operator [] to obtain the Value according to the key, and use GetXXX of the Value Methods get different types of data values.
//获取JSON中数组的方法(宠物数组)[dog,cat] const rapidjson::Value & v=d1["pets"]; for(unsigned int i=0;i<v.Size();++i){ const rapidjson::Value & val=v[i]; log("%s",val.GetString()); } //根据key获得value(学生信息)"stuInfo":{"stuAge":23,"stuName":"rose","birthday":"1990-01-12"} const rapidjson::Value & v2=d1["stuInfo"]; //获得整型值 const rapidjson::Value&val1 = v2["stuAge"]; log("val.GetString()=%d",vall.GetInt()); //获得字符串值 const rapidjson::Value&val2 = v2["stuName"]; log("val.GetString()=%s",val2.GetString()); //获得字符串值 const rapidjson::Value&val3 = v2["birthday"]; log("val.GetString()=%s",val3.GetString()); //根据key获得value(other)"other":[true,30] const rapidjson::Value&v3=d1["other"]; for(unsigned int i=0;i<v3.Size();++i){ const rapidjson::Value&val=v3[i]; if(val.IsBool()){ log("%d",val.GetBool()); } if(val.IsInt()){ log("%d",val.GetInt()); } }
The above is the detailed content of What is json data parsing. For more information, please follow other related articles on the PHP Chinese website!