What is json data parsing
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

The Gson@Expose annotation can be used to mark whether a field is exposed (contained or not) for serialization or deserialization. The @Expose annotation can take two parameters, each parameter is a boolean value and can take the value true or false. In order for GSON to react to the @Expose annotation, we have to create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, which configures Gson to exclude all fields without Expose annotation from serialization or deserialization. Syntax publicGsonBuilderexclud

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string. When writing programs in Golang, we often need to convert the structure into a JSON string. In this process, the json.MarshalIndent function can help us. Implement formatted output. Below we will explain in detail how to use this function and provide specific code examples. First, let's create a structure containing some data. The following is an indication

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Using PHP's json_encode() function to convert an array or object into a JSON string and format the output can make it easier to transfer and exchange data between different platforms and languages. This article will introduce the basic usage of the json_encode() function and how to format and output a JSON string. 1. Basic usage of json_encode() function The basic syntax of json_encode() function is as follows: stringjson_encod