search
HomeCommon ProblemWhat is json data parsing

What is json data parsing

May 20, 2019 am 09:36 AM
json

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.

What is json data parsing

# 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>Document;

typedef GenericValue>Value;

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(*this)[name];}

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(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 const log for></v.size>

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!