zoukankan      html  css  js  c++  java
  • android cocos2dx libjson

    说一说关于libjson在android上的编译问题
    在这里使用的是libjson_7.6.1版本  下载地址:http://sourceforge.net/projects/libjson/files/ ,也可以访问http://json.org.
    接下来将source文件夹和JSONOption.h和libjson.h文件复制到自己的项目当中去,并且在android.mk中将需要编译的文件添加进来,进行编译,报错:

    jni/http://www.cnblogs.com/Classes/libjson/_internal/Source/JSONNode.cpp:168:5: error: exception handling disabled, use -fexceptions to enable

    大致意思是说需要将c++异常捕获打开  所以需要再android.mk文件的 include $(CLEAR_VARS) 下方添加一句"LOCAL_CPPFLAGS += -fexceptions"

    再次进行编译 成功!

    接下来说下使用libjson解析json数组,代码如下

    void HelloWorld::testJson()
    {
        
        const char* str="[[\"tiner\",10,\"NBA\"],[\"james\",0621,\"CHINA\"]]";
        JSONNode* node=(JSONNode*)json_parse(str);
        if (node == NULL)
        {
            printf("Invalid JSON Node1\n");
            return;
        }
        
        JSONNODE_ITERATOR i = json_begin(node);
        while (i != json_end(node))
        {
            if (*i == NULL)
            {
                printf("Invalid JSON Node2\n");
                return;
            }
            
            // recursively call ourselves to dig deeper into the tree
            if (json_type(*i) == JSON_ARRAY)
            {
                parseArrJSON(*i);
            }
            
            i++;
        }
        
    
    }
    
    void HelloWorld::parseArrJSON(JSONNODE *n)
    {
        if (n == NULL)
        {
            CCLOG("Invalid JSON Node\n");
            return;
        }
        
        JSONNODE_ITERATOR it = json_begin(n);
        while (it != json_end(n))
        {
            if (*it == NULL)
            {
                CCLOG("Invalid JSON Node\n");
                return;
            }
            
            CCLog("NAME:%s",json_as_string(*it));it++;
            CCLog("NUMBER:%d",json_as_int(*it));it++;
            CCLog("WHERE:%s",json_as_string(*it));
            
            ++it;
        }
        
    }

    输出:

    Cocos2d: NAME:tiner

    Cocos2d: NUMBER:10

    Cocos2d: WHERE:NBA

    Cocos2d: NAME:james

    Cocos2d: NUMBER:621

    Cocos2d: WHERE:CHINA

  • 相关阅读:
    Redis使用详细教程
    Web API 强势入门指南
    log4net
    ASP.NET Web API——选择Web API还是WCF
    Hadoop RPC机制
    力扣算法:每日温度
    力扣算法:完全平方数
    力扣算法:岛屿数量
    面试总结二
    面试总结
  • 原文地址:https://www.cnblogs.com/mokey/p/3072158.html
Copyright © 2011-2022 走看看