当前位置:编程学习 > JS >>

Cocos2d-x学习(五):cocos2d-x解析json(使用libjson库)

cocos2d-x很好,扩展性很好,但是其第三方库还不是很成熟,正如王哲所说,需要我们自己寻找,比如json解析和格式化。

 


我们的网络部分需要用到json,网络部分用的比较多的就是json,毕竟很轻量级么。。。上午我看到cocos2d-x论坛里关于json的第三方库有人提到了两个,一个是jsoncpp,一个是libjson,我上午首选的是jsoncpp,看其编译过程,是比较复杂的,我耐着性子又下了scons(编译器),编是编出来了,可是在iphone上会报link的错,不知其解,在Mac app中使用,正常。。。擦,原来是Mac是64位的,而iphone需要的是32位库的,又是各种找答案,终于在其官网的wiki上找到解答-------在Mac 64位上编出32位库的方法,试了,发现编不过去,好像是gcc版本的原因,再看下面的warning,上面说对Mac的支持就是不怎么样!好吧,我承认它不是跨iphone和android的最佳选择了。(在ubuntu上编译很轻松,没问题的,应该android上没问题),于是下午我就转向了libjson。

 


1.json介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。


JSON建构有两种结构:

  1. “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。   2.值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。


(ps:这两段是从百度百科上copy的,但是json的第2种结构很重要,是人们常常忽视的)

 


2.编译
论坛里也有人提问过libjson怎么用,王哲给出的答案是直接copy源码到工程里,想必没有太复杂的编译参数,环境之类的障碍,于是我就copy了,编译,没错!工程如图

(copy了libjson下的Source,Dependencies目录和JSONOptions.h,libjson.h)

 \


(PS: 只所以把光标放在JSONOptions.h上,是因为它很邪恶,几个宏和其开关的用法,源码竟然还有个bug。。。)

 


3.用法
libjson为我们提供了两套接口,一套c的,一套c++的,在其目录的Getting Started目录下,至于选择就因人喜好了!

 \


(1)简单写法:

[cpp]
<span style="font-size:16px;">JSONNODE *n = json_new(JSON_NODE); 
    json_push_back(n, json_new_i("number", 88)); 
    json_push_back(n, json_new_a("string", "oneRain")); 
    json_push_back(n, json_new_a("charactor", "中文")); 
     
    json_char *jc = json_write_formatted(n); 
    CCLOG("json: %s", jc); 
    json_free(jc); 
    json_delete(n);</span> 
<span style="font-size:16px;">JSONNODE *n = json_new(JSON_NODE);
    json_push_back(n, json_new_i("number", 88));
    json_push_back(n, json_new_a("string", "oneRain"));
    json_push_back(n, json_new_a("charactor", "中文"));
   
    json_char *jc = json_write_formatted(n);
    CCLOG("json: %s", jc);
    json_free(jc);
    json_delete(n);</span>输出结果:

 


(2)简单读法:
读取就需要一个简单的解析过程了,当然也可以没有,如果没有键的话


[cpp]
<span style="font-size:16px;">void ParseJSON(JSONNODE *n) 

    if (n == NULL){ 
        printf("Invalid JSON Node1\n"); 
        return; 
    } 
     
    JSONNODE_ITERATOR i = json_begin(n); 
    while (i != json_end(n)){ 
        if (*i == NULL){ 
            printf("Invalid JSON Node2\n"); 
            return; 
        } 
         
        // recursively call ourselves to dig deeper into the tree  
        if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ 
            ParseJSON(*i); 
        } 
         
        // get the node name and value as a string  
        json_char *node_name = json_name(*i); 
         
        // find out where to store the values  
        if (strcmp(node_name, "number") == 0){ 
            json_int_t node_value = json_as_int(*i); 
            CCLOG("%d", node_value); 
        } 
        else if (strcmp(node_name, "string") == 0){ 
            json_char *node_value = json_as_string(*i); 
            CCLOG("%s", node_value); 
            json_free(node_value); 
        } 
        else if (strcmp(node_name, "charactor") == 0) 
            CCLOG("%s", json_as_string(*i)); 
         
        // cleanup and increment the iterator  
        json_free(node_name); 
        ++i; 
    } 
}</span> 
<span style="font-size:16px;">void ParseJSON(JSONNODE *n)
{
    if (n == NULL){
        printf("Invalid JSON Node1\n");
        return;
    }
   
    JSONNODE_ITERATOR i = json_begin(n);
    while (i != json_end(n)){
        if (*i == NULL){
            printf("Invalid JSON Node2\n");
            return;
        }
       
        // recursively call oursel

补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,