【存储】Cocos2d-x使用JsonCpp


【说明】

JsonCpp使用起来很方便,集成也比较简单,这里总结一下我继承的步骤,和封装的类。


【下载】

https://github.com/open-source-parsers/jsoncpp


【集成】

1. 下载jsoncpp后解压,将 ”include“ 和 ”src“ 目录拷贝到项目中,src目录中只需要 ”lib_json“文件夹里面的内容。我为了简化目录,直接将头文件(include/json)放到 "jsoncpp/include" 中,将实现文件(src/lib_json)放到 "jsoncpp/src" 中。

我的目录结构:
jsoncpp/include
jsoncpp/src
LJson.h
LJson.cpp
Android.mk

2. Xcode直接将jsoncpp引入工程即可,Android可将C文件加入jni/Android.mk。我采用的是单独编译的方式,写了一个Android.mk文件,在jni/Android.mk中引用。

LOCAL_PATH := $(call my-dir)

#清理变量定义
include $(CLEAR_VARS)

#模块名称
LOCAL_MODULE := jsoncpp_static

#库文件名称
LOCAL_MODULE_FILENAME := libjsoncpp

#源文件
LOCAL_SRC_FILES := \
jsoncpp/src/json_reader.cpp \
jsoncpp/src/json_value.cpp \
jsoncpp/src/json_writer.cpp \
LJson.cpp

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/jsoncpp/include

#头文件目录
LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsoncpp/include

#构建静态库
include $(BUILD_STATIC_LIBRARY)


【使用】

为了方便使用,我封装了个类,把Json的操作都放到类里面完成,直接上代码。

//=================================================================================================
// LJson.h
// Created by Dolphin Lee.
//=================================================================================================
#ifndef __L_JSON_H__
#define __L_JSON_H__

#include "cocos2d.h"
USING_NS_CC;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "json.h"
#else
#include "jsoncpp/include/json.h"
#endif

// LJsonReader
//=================================================================================================

class LJsonReader
{
public:
    // Create and init, from json file or json string.
    LJsonReader(const std::string& json, bool isFile = false);
    
    // get bool value.
    bool value_bool(const std::string& key);
    bool value_bool(const std::string& key, const std::string& node);
    bool value_bool(const std::string& key, const std::string& node1, const std::string& node2);
    
    // get int value.
    int value_int(const std::string& key);
    int value_int(const std::string& key, const std::string& node);
    int value_int(const std::string& key, const std::string& node1, const std::string& node2);
    
    // get string value.
    std::string value_string(const std::string& key);
    std::string value_string(const std::string& key, const std::string& node);
    std::string value_string(const std::string& key, const std::string& node1, const std::string& node2);
    
    // get float value.
    float value_float(const std::string& key);
    float value_float(const std::string& key, const std::string& node);
    float value_float(const std::string& key, const std::string& node1, const std::string& node2);
    
    // get double value.
    double value_double(const std::string& key);
    double value_double(const std::string& key, const std::string& node);
    double value_double(const std::string& key, const std::string& node1, const std::string& node2);
    
private:
    Json::Value _root;
};

// LJsonWriter
//=================================================================================================

class LJsonWriter
{
public:
    // Create and init.
    LJsonWriter();
    
    // Add value to json.
    void add_bool(const std::string& key, bool value);
    void add_int(const std::string& key, int value);
    void add_string(const std::string& key, std::string value);
    void add_float(const std::string& key, float value);
    void add_double(const std::string& key, double value);
    
    // get json data.
    std::string json_data();
    
private:
    Json::Value _root;
};

//=================================================================================================

#endif
//=================================================================================================
// LJson.cpp
//=================================================================================================
#include "LJson.h"

//=================================================================================================
// LJson_Reader
//=================================================================================================

//=================================================================================================
// Create
//=================================================================================================

LJsonReader::LJsonReader(const std::string& json, bool isFile)
{
    // Get json string.
    std::string str = json;
    if (isFile)
    {
        str = FileUtils::getInstance()->getStringFromFile(json);
    }
    
    // Read and parse.
    Json::Reader reader;
    //Json::Value root;
    if (!reader.parse(str, _root, false))
    {
        CCLOG("[Json]:read json faild.");
    }
}

//=================================================================================================
// Bool
//=================================================================================================

bool LJsonReader::value_bool(const std::string& key)
{
    if (_root[key].isNull())
    {
        return false;
    }
    return _root[key].asBool();
}

bool LJsonReader::value_bool(const std::string& key, const std::string& node)
{
    if (_root[node][key].isNull())
    {
        return false;
    }
    return _root[node][key].asBool();
}

bool LJsonReader::value_bool(const std::string& key, const std::string& node1, const std::string& node2)
{
    if (_root[node1][node2][key].isNull())
    {
        return false;
    }
    return _root[node1][node2][key].asBool();
}

//=================================================================================================
// Int
//=================================================================================================

int LJsonReader::value_int(const std::string& key)
{
    if (_root[key].isNull())
    {
        return 0;
    }
    return _root[key].asInt();
}

int LJsonReader::value_int(const std::string& key, const std::string& node)
{
    if (_root[node][key].isNull())
    {
        return 0;
    }
    return _root[node][key].asInt();
}

int LJsonReader::value_int(const std::string& key, const std::string& node1, const std::string& node2)
{
    if (_root[node1][node2][key].isNull())
    {
        return 0;
    }
    return _root[node1][node2][key].asInt();
}

//=================================================================================================
// String
//=================================================================================================

std::string LJsonReader::value_string(const std::string& key)
{
    if (_root[key].isNull())
    {
        return "";
    }
    //_root.get(key, "null").asString();
    return _root[key].asString();
}

std::string LJsonReader::value_string(const std::string& key, const std::string& node)
{
    if (_root[node][key].isNull())
    {
        return "";
    }
    return _root[node][key].asString();
}

std::string LJsonReader::value_string(const std::string& key, const std::string& node1, const std::string& node2)
{
    if (_root[node1][node2][key].isNull())
    {
        return "";
    }
    return _root[node1][node2][key].asString();
}

//=================================================================================================
// Float
//=================================================================================================

float LJsonReader::value_float(const std::string& key)
{
    if (_root[key].isNull())
    {
        return 0;
    }
    return _root[key].asFloat();
}

float LJsonReader::value_float(const std::string& key, const std::string& node)
{
    if (_root[node][key].isNull())
    {
        return 0;
    }
    return _root[node][key].asFloat();
}

float LJsonReader::value_float(const std::string& key, const std::string& node1, const std::string& node2)
{
    if (_root[node1][node2][key].isNull())
    {
        return 0;
    }
    return _root[node1][node2][key].asFloat();
}

//=================================================================================================
// Double
//=================================================================================================

double LJsonReader::value_double(const std::string& key)
{
    if (_root[key].isNull())
    {
        return 0;
    }
    return _root[key].asDouble();
}

double LJsonReader::value_double(const std::string& key, const std::string& node)
{
    if (_root[node][key].isNull())
    {
        return 0;
    }
    return _root[node][key].asDouble();
}

double LJsonReader::value_double(const std::string& key, const std::string& node1, const std::string& node2)
{
    if (_root[node1][node2][key].isNull())
    {
        return 0;
    }
    return _root[node1][node2][key].asDouble();
}

//=================================================================================================
// LJson_Writer
//=================================================================================================

//=================================================================================================
// Create
//=================================================================================================

LJsonWriter::LJsonWriter()
{
    
}

//=================================================================================================
// Add
//=================================================================================================

void LJsonWriter::add_bool(const std::string& key, bool value)
{
    _root[key] = value;
}

void LJsonWriter::add_int(const std::string& key, int value)
{
    _root[key] = value;
}

void LJsonWriter::add_string(const std::string& key, std::string value)
{
    _root[key] = value;
}

void LJsonWriter::add_float(const std::string& key, float value)
{
    _root[key] = value;
}

void LJsonWriter::add_double(const std::string& key, double value)
{
    _root[key] = value;
}

//=================================================================================================
// Data
//=================================================================================================

std::string LJsonWriter::json_data()
{
    Json::FastWriter writer;
    return writer.write(_root);
}

//=================================================================================================
//
//=================================================================================================

使用起来很方便,代码如下:

std::string str = "{\"Name\":\"Dolphin\",\"ID\":1007}";

// 从json文件或字符串读取
//LJsonReader reader("test.json", true);
LJsonReader reader(str);

std::string name = reader.value_string("Name");
std::string id = reader.value_int("ID");

// 构建Json字符串
LJsonWriter writer;
writer.add_string("Name", "晴天");
writer.add_int("ID", 1007);

std::string str = writer.json_data();


【参考】

http://blog.csdn.net/cocos2der/article/details/8061888



猜你喜欢

转载自blog.csdn.net/ldpjay/article/details/46442903
今日推荐