c++,或者cococ2dx怎么利用jsonbox写入数据到json文件

下面代码写入到json文件的结果是

{

"BluetoothInfo":

[

"devicename":"HTC1",

"deciceStatus":1,

"deviceType":2,

"deviceID":"D8:Y3:54:67"

]


}


/////写入json数据

void BlueToothView::writeJsonDataFromJsonFile(const std::string &jsonFile)

{

    /*写入文件*/

    JsonBox::Object key;

    JsonBox::Array arrayJson;


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    for (int j=0; j<mEnableUseDeviceList->count(); j++)

    {

        JsonBox::Object value;

        BlueToothData * tempData = dynamic_cast<BlueToothData *>(mEnableUseDeviceList->getObjectAtIndex(j));

        value["devicename"] = JsonBox::Value(tempData->deviceName.c_str());

        value["deciceStatus"] = JsonBox::Value(tempData->status);

        value["deviceType"] = JsonBox::Value(tempData->type);

        value["deviceID"] = JsonBox::Value(tempData->deviceID.c_str());

        arrayJson.push_back(value);

    }

#else

    for(int i=0;i<4;i++)

    {

        JsonBox::Object value;

        value["devicename"] = JsonBox::Value(StringUtils::format("HTC%d",i));

        int status =random(1,3);

        value["deciceStatus"] = JsonBox::Value(status);

        int type =random(1,3);

        value["deviceType"] = JsonBox::Value(type);

        value["deviceID"] = JsonBox::Value("D8:Y3:54:67");

        arrayJson.push_back(value);

    }

#endif

    key["BluetoothInfo"]=arrayJson;

    JsonBox::Value v(key);

    JGUtil::saveDataToJson(v, jsonFile);

}


备注:

void JGUtil::readJson(JsonBox::Value &json,const std::string filename) {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    JGUtil::copyData(filename.c_str());

    std::string s_filepath = CCFileUtils::sharedFileUtils()->getWritablePath() + filename;

    json.loadFromFile(s_filepath.c_str());

#else

    std::string s_filepath = FileUtils::getInstance()->fullPathForFilename(filename);

    log("claudis filename = %s",s_filepath.c_str());

    json.loadFromFile(s_filepath.c_str());

    //log("claudis %s",json.getString().c_str());

#endif

}

void JGUtil::saveDataToJson(JsonBox::Value &json,const std::string filename) {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    

    std::string s_filepath = CCFileUtils::sharedFileUtils()->getWritablePath() + filename;

    json.writeToFile(s_filepath);

#else

    std::string s_filepath = FileUtils::getInstance()->fullPathForFilename(filename);

    json.writeToFile(s_filepath);

 

#endif

}



你可能感兴趣的:(c++,或者cococ2dx怎么利用jsonbox写入数据到json文件)