There are three ways in which data storage is generally
1>, database
2>, file
3> the network
Here I introduce the JSON format and the XML format of the file storage
Let's talk about the format of commonly used file storage data
1>,json format
2>,xml format
I think the difference between these two formats is that JSON is suitable for storing small data, and XML is suitable for storing big data.
In Cocos2d-x, if we are going to use JSON first we need to first import a JSON library: Libjson
The demo is as follows:
To add a Libjson header file
To create data in JSON format:
Here with Jsonnode, define a parent node n, and then define a node array c, named Childnode, and then press the data into the child node, the node is pressed into the array, and then the array is added to the parent node
This creates a JSON string
Let's look at the parse JSON again:
Instead of parsing the JSON string into a file, I use the write_formatted () function to convert it to a JSON-formatted string
Next create and write a new file:
If there is a file in the sandbox, it will be written directly to the file, and if it does not, the file is created and then written
Read the file again:
Here are relative to the sandbox, not relative to the engineering resources
Re-initialize the function to call these methods.
Look at the results of the operation:
So we can save the data in JSON format in the file.
XML file, I'm here to talk about its parsing.
First we create a new Cocos2d-x project and then import an XML library: TinyXML
Add a Test.xml file to the project:
We define a person class to receive and print each set of data:
Person.h
Person.cpp
We parse the XML inside the main class:
Finally, it is called directly in the main function.
Data store-Customize JSON string write file to read data in XML format