Data/configuration storage method JSON article take cocos2d for iPhone + touchjson as an Example

Source: Internet
Author: User

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Discuss newsgroups and documents

Preface

For the configuration benefits, JSON introduction, and why to use JSON, see the original jsoncpp article.
. This article only provides some explanations for touchjson.

 

Cocos2d for iPhone
+ Touchjson

Cocos2d for iPhone is the most comprehensive open-source 2D engine that I have ever seen, even if it is not the fastest. Because it only supports the iPhone platform, it can do well on this platform. Now the new version of cocos2d for iPhone supports ios4 and iphone4, and the installation and use of its engineering template is also very convenient. The new version even changed the old lgpl protocol to the current mit Protocol, making it more flexible and free to use ....
Touchjson does not want to add c ++ code in a relatively complete Objective C environment (in fact, it cannot be avoided if box2d is used). Therefore, jsoncpp is not used in combination with cocos2d, what's more, the template project of cocos2d has touchjson. It is too inhuman to delete it and insert it into jsoncpp .... Haha. In addition, because touchjson is completed with Objective C and the parsed object is also an nsdictionary object, it is more natural to use the object combination with objecitve C. It is developed for Apple using Apple, I am almost used to a whole set of unique features on the Apple platform .... Compared with those in the past, C # is easy to understand because it is completely in the hands of MS and does not want to learn. This is also a pity, because Objective C is more closed than C #, and, at least C # represents the most beautiful and advanced syntax in the world.

First, use the cocos2d template to create a new project. The default effect is to display a hello World. As follows:

Here, we don't need any other figure. Let's see how to configure this Hello world.

The most basic process:
Create a JSON file with only two lines of configuration. One line indicates the displayed text and one line indicates the rotation of the text.
{


"Text"
: "Don't hello World"
,
"Rotation"
: 20
}



Then, put the JSON file into the resources directory of the Project. Here I name it picture. JSON.

Then you can start parsing the JSON file.
The entire parsing process involves several steps. First, # import "cjsondeserializer. H"
Then, obtain the location of the files in the resources directory after compilation and packaging:
Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "picture" oftype: @ "JSON"];

After obtaining the file path, read data from the file:
Nsdata * jsondata = [[nsfilemanager defaultmanager] contentsatpath: path];

After obtaining the file data, parse the JSON file:
// Parse JSON results with touchjson. It converts it into a dictionary.
Cjsondeserializer * jsondeserializer = [cjsondeserializer deserializer];
Nserror * error = nil;
Nsdictionary * jsondict = [jsondeserializer deserializeasdictionary: jsondata error: & error];
If (error ){
// Handle error, didn't have here.
}


In this case, jsondict stores the parsed JSON data.
(The above Code is added to the init of helloworldscene)

See the following usage:
First, text changes the text displayed by helloworld:
Nsstring * text = [jsondict valueforkey: @ "text"];
// Create and initialize a label
Cclabel * label = [cclabel labelwithstring: Text fontname: @ "marker felt" fontsize: 64];
Nsdictionary in cocoa
How to use it.

Then, rotation changes the rotation:
Nsnumber * rotation = [jsondict valueforkey: @ "rotation"];
Nsassert (rotation, @ "didn't have a key named rotation ");
Label. Rotation = [rotation floatvalue];

Everything is ready, see the effect:

The above process has been basically complete, as a supplement, or an example of adding a JSON array.
In touchjson, it is not recommended that you set the root object as an array.
, Touchjson). In fact, this is not the case. We can use a key to specify this array.
Therefore, the JSON file is defined as follows:

{


"Result"
:
[


{


"Text"
: "Don't hello World"
,

"Rotation"
: 20

}

,
{


"Text"
: "Just hello World"
,
"Rotation"
:-20
}


]


}

Then, an nsdictionary object is read first, but then the array is retrieved:
Nsarray * dictarray = [jsondict valueforkey: @ "result"];

Then traverse the array. At this time, each object in the array is an nsdictionary object.

For (nsdictionary * dict in dictarray ){}

In this case, the object obtained from nsdictionary is similar to the original dictionary object. You can use the valueforkey to obtain the corresponding configuration. The complete loop code is as follows:

For (nsdictionary * dict in dictarray ){
Nsstring * text = [dict valueforkey: @ "text"];
// Create and initialize a label
Cclabel * label = [cclabel labelwithstring: Text fontname: @ "marker felt" fontsize: 64];

Nsnumber * rotation = [dict valueforkey: @ "rotation"];
Nsassert (rotation, @ "didn't have a key named rotation ");
Label. Rotation = [rotation floatvalue];

// Ask director the window size
Cgsize size = [[ccdirector shareddire] winsize];

// Position the label on the center of the screen
Label. Position = CCP (size. width/2, size. Height/2 );

// Add the label as a child to this layer
[Self addchild: Label];
}

You can see the effect of displaying multiple texts at the same time:

Summary:
After using jsoncpp and touchjson, we can find that the JSON data structure is mainly a key: Value ing and an array, so whether in C ++ or in Objective C, it can always be expressed with the native structure of the language (MAP in C ++, nsdictionary and nsarray in Objective C), so it is very convenient to use, compared with XML, XML is powerful and complex. Simple JSON completes the configuration task while keeping the concept simple.

 

 

 

 

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.