cocos2dx从json文件解析数据

#include "json/rapidjson.h"
#include "json/document.h"

std::string getPlatformServerAddress()
{
    std::string address(PLATFORM_SERVER_ADDRESS);

#ifdef _DEBUG
    address = getPlatformAddressFromConfig();
    if (address.empty())
    {
        address = PLATFORM_SERVER_ADDRESS;
    }
#endif

    if(NA::NASocket::isValidIP(address))
    {
        return address;
    }
    else
    {
        std::vector ips = NA::NASocket::getIpAddress(address.c_str());
        return ips.empty() ? "" : ips.at(0);
    }
}

std::string getPlatformAddressFromConfig()
{
    std::string debugip;
    do 
    {
        std::string configfile("config.json");
        if (cocos2d::FileUtils::getInstance()->isFileExist(configfile))
        {
            std::string json = cocos2d::FileUtils::getInstance()->getStringFromFile(configfile);
            rapidjson::Document doc;
            doc.Parse(json.c_str());
            if (doc.HasParseError() || !doc.IsObject())
            {
                break;
            }
            if (doc.HasMember("debugip"))
            {
                debugip = doc["debugip"].GetString();
                break;
            }
        }
    } while (0);
    return debugip;
}
{
    "debugip":"192.168.1.89",
}

你可能感兴趣的:(cocos2dx从json文件解析数据)