Json数据操作 详细版

Json数据操作http://blog.51cto.com/shahdza/1614604


 //文件拷贝

        //获取可读可写区域

        string path=FileUtils::getInstance()->getWritablePath();

        //拼接路径      并没有创造

        string jsonPath=path+string("charpter2.json");

        

        cout<<jsonPath<<endl;

        //获取 资源 里面的"charpter.json"路劲

      string path2=FileUtils::getInstance()->fullPathForFilename("charpter2.json");

        

        //判断可读可写区域是否有     "charpter.json"路径

        if (!FileUtils::getInstance()->isFileExist(jsonPath) )

        {

            //获取资源

            string fileContents=FileUtils::getInstance()->getStringFromFile(path2);

            //打开或创造

            FILE *file =fopen(jsonPath.c_str(), "w");

            

            if (file)

            {

                //字符串输出

                //fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

                fputs(fileContents.c_str() , file);

                //保存 关闭

                fclose(file);

            }

            

        }

 //json解析

        //获取 可读可写 路径下面"charpter.json"文件内容

        string date=FileUtils::getInstance()->getStringFromFile(jsonPath);

        

        //解析

        Document doc;//解析工具

        

        doc.Parse<kParseCommentsFlag>( date.c_str());

        if (doc.HasParseError())

        {

            log("json  error\n");

            //        return;

        }

        //如果存在

        if (doc.HasMember("gameCharpter"))

        {

            rapidjson::Value&_gameCharpter=doc["gameCharpter"];

            for (int i=0; i<_gameCharpter.Size(); ++i)

            {

                rapidjson::Value&_element=_gameCharpter[i];

                

                rapidjson::Value&_CharpterNum=_element["Charpter"];

                log("Charpter:%d\n",_CharpterNum.GetInt());

                

                

                rapidjson::Value&_case=_element["Case"];

                for (int j=0; j<_case.Size(); ++j)

                {

                    rapidjson::Value&_item=_case[j];

                    rapidjson::Value&_item1=_item["case"];

                    log("case:%d/n",_item1.GetInt());

                    

                    rapidjson::Value&_item2=_item["isLock"];

                    log("isLock:%d/n",_item2.GetBool());

                    

                    rapidjson::Value&_item3=_item["HighScore"];

                    log("HighScore:%d/n",_item3.GetInt());

                    

                    rapidjson::Value&_item4=_item["Star"];

                    log("Star:%d/n",_item4.GetInt());

                    

                    rapidjson::Value&_item5=_item["BirdsNum"];

                    log("BirdsNum:%d/n",_item5.GetInt());

                    

                    rapidjson::Value&_item6=_item["PigNum"];

                    log("PigNum:%d/n",_item6.GetInt());

                    

                    rapidjson::Value&_item7=_item["IceNum"];

                    log("IceNum:%d/n",_item7.GetInt());

                    

                    rapidjson::Value&_item8=_item["BirdPoint"];

                    for (int q=0;q<_item8.Size() ; ++q)

                    {

                        rapidjson::Value&BirdsNum=_item8[q];

                        string str=StringUtils::format("Point%d",q+1);

                        rapidjson::Value&BirdsNum1=BirdsNum[str.c_str()];

                        rapidjson::Value&BirdsNum10=BirdsNum1[0]["X"];

                        rapidjson::Value&BirdsNum11=BirdsNum1[0]["Y"];

                       log(" X:%d,Y:%d/n",BirdsNum10.GetInt(),BirdsNum11.GetInt());

                   

                    }

                    

                    rapidjson::Value&_item9=_item["PigPoint"];

                    for (int w=0;w<_item9.Size() ; ++w)

                    {

                        rapidjson::Value&BirdsNum=_item9[w];

                        string str=StringUtils::format("Point%d",w+1);

                        rapidjson::Value&BirdsNum1=BirdsNum[str.c_str()];

                    

                        rapidjson::Value&BirdsNum10=BirdsNum1[0]["X"];

                        rapidjson::Value&BirdsNum11=BirdsNum1[0]["Y"];

                        log(" X:%d,Y:%d/n",BirdsNum10.GetInt(),BirdsNum11.GetInt());

                    }

                    

                    

                    rapidjson::Value&_item10=_item["IcePoint"];

                    for (int w=0;w<_item10.Size() ; ++w)

                    {

                        rapidjson::Value&BirdsNum=_item10[w];

                        

                        string str=StringUtils::format("Point%d",w+1);

                        rapidjson::Value&BirdsNum1=BirdsNum[str.c_str()];

                        rapidjson::Value&BirdsNum10=BirdsNum1[0]["X"];

                        rapidjson::Value&BirdsNum11=BirdsNum1[0]["Y"];

                        log("X:%d,Y:%d/n",BirdsNum10.GetInt(),BirdsNum11.GetInt());

                    }

                    

                }

  

            }

        }


修改:

   

 doc["gameCharpter"][chapter-1]["Case"][Case-1]["isLock"].SetBool(true);


保存:

    rapidjson::StringBuffer buffer;

    rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);

    doc.Accept(writer);

    FILE*file=fopen(jsonPath.c_str(), "wb");

    if (file)

    {

        fputs(buffer.GetString(), file);

        fclose(file);

    }


charpter2.json

解析网站http://www.json.cn

例如:

{

"gameCharpter":[

{

"Charpter":1,

"Case":[

        {

        "case":1,

        "isLock":false,

            "HighScore":360,

            "Star":0,

            "BirdsNum":4,

            "PigNum":4,

            "IceNum":6,

            "BirdPoint":[

            {"Point1":[{"X":165,"Y":38}]},

            {"Point2":[{"X":120,"Y":38}]},

            {"Point3":[{"X":75,"Y":38}]},

            {"Point4":[{"X":30,"Y":38}]}

            ],

            "PigPoint":[

            {"Point1":[{"X":279,"Y":38}]},

            {"Point2":[{"X":250,"Y":97}]},

            {"Point3":[{"X":279,"Y":125}]},

            {"Point4":[{"X":308,"Y":96}]}


            ],

            "IcePoint":[

            {"Point1":[{"X":250,"Y":38}]},

            {"Point2":[{"X":250,"Y":67}]},

            {"Point3":[{"X":310,"Y":38}]},

            {"Point4":[{"X":310,"Y":67}]},

            {"Point5":[{"X":280,"Y":67}]},

            {"Point6":[{"X":280,"Y":95}]}


            ]

        },

    {

    "case":2,

        "isLock":true,

        "HighScore":0,

        "Star":0,

        "BirdsNum":3,

        "PigNum":5,

        "IceNum":10,

        "BirdPoint":[

        {"Point1":[{"X":160,"Y":38}]},

        {"Point2":[{"X":115,"Y":38}]},

        {"Point3":[{"X":75,"Y":38}]}

        ],

        "PigPoint":[

        {"Point1":[{"X":250,"Y":154}]},

        {"Point2":[{"X":279,"Y":154}]},

        {"Point3":[{"X":308,"Y":96}]},

        {"Point4":[{"X":337,"Y":67}]},

        {"Point5":[{"X":366,"Y":38}]}

        ],

        "IcePoint":[

        {"Point1":[{"X":250,"Y":126}]},

        {"Point2":[{"X":250,"Y":90}]},

        {"Point3":[{"X":250,"Y":64}]},

        {"Point4":[{"X":250,"Y":38}]},

        {"Point5":[{"X":279,"Y":96}]},

        {"Point6":[{"X":279,"Y":67}]},

        {"Point7":[{"X":279,"Y":38}]},

        {"Point8":[{"X":308,"Y":66}]},

        {"Point9":[{"X":308,"Y":38}]},

        {"Point10":[{"X":337,"Y":38}]}

        ]

    },

    {

      "case":3,

        "isLock":true,

        "HighScore":0,

        "Star":0,

        "BirdsNum":2,

        "PigNum":2,

        "IceNum":8,

        "BirdPoint":[

        {"Point1":[{"X":160,"Y":38}]},

        {"Point2":[{"X":115,"Y":38}]}

        ],

        "PigPoint":[

        {"Point1":[{"X":277,"Y":65}]},

        {"Point2":[{"X":277,"Y":119}]}

        ],

        "IcePoint":[

        {"Point1":[{"X":250,"Y":38}]},

        {"Point2":[{"X":250,"Y":65}]},

        {"Point3":[{"X":250,"Y":92}]},

        {"Point4":[{"X":277,"Y":38}]},

        {"Point5":[{"X":277,"Y":92}]},

        {"Point6":[{"X":304,"Y":38}]},

        {"Point7":[{"X":304,"Y":65}]},

        {"Point8":[{"X":304,"Y":92}]}

        ]

    }

]

},


{"Charpter":2,

    "Case":[

    {

    "case":4,

    "isLock":true,

    "HighScore":0,

    "Star":0,

    "BirdsNum":3,

    "PigNum":4,

    "IceNum":5,

    "BirdPoint":[

    {"Point1":[{"X":160,"Y":38}]},

    {"Point2":[{"X":115,"Y":38}]},

    {"Point3":[{"X":75,"Y":38}]}

    ],

    "PigPoint":[

    {"Point1":[{"X":250,"Y":38}]},

    {"Point2":[{"X":275,"Y":63}]},

    {"Point3":[{"X":325,"Y":63}]},

    {"Point4":[{"X":350,"Y":38}]}

    ],

    "IcePoint":[

    {"Point1":[{"X":275,"Y":38}]},

    {"Point2":[{"X":300,"Y":38}]},

    {"Point3":[{"X":300,"Y":63}]},

    {"Point4":[{"X":300,"Y":88}]},

    {"Point5":[{"X":325,"Y":38}]}

    ]

    },

{

"case":5,

    "isLock":true,

    "HighScore":0,

    "Star":0,

    "BirdsNum":4,

    "PigNum":4,

    "IceNum":11,

    "BirdPoint":[

    {"Point1":[{"X":160,"Y":38}]},

    {"Point2":[{"X":115,"Y":38}]},

    {"Point3":[{"X":75,"Y":38}]},

    {"Point4":[{"X":35,"Y":38}]}


    ],

    "PigPoint":[

    {"Point1":[{"X":210,"Y":38}]},

    {"Point2":[{"X":238,"Y":122}]},

    {"Point3":[{"X":280,"Y":122}]},

    {"Point4":[{"X":418,"Y":66}]}

    ],

    "IcePoint":[

    {"Point1":[{"X":210,"Y":66}]},

    {"Point2":[{"X":238,"Y":38}]},

    {"Point3":[{"X":238,"Y":66}]},

    {"Point4":[{"X":238,"Y":94}]},

    {"Point5":[{"X":266,"Y":38}]},

    {"Point6":[{"X":266,"Y":66}]},

    {"Point7":[{"X":294,"Y":38}]},

    {"Point8":[{"X":294,"Y":66}]},

    {"Point9":[{"X":390,"Y":38}]},

    {"Point10":[{"X":390,"Y":66}]},

    {"Point11":[{"X":418,"Y":38}]}

    ]

},

{

"case":6,

    "isLock":true,

    "HighScore":0,

    "Star":0,

    "BirdsNum":3,

    "PigNum":3,

    "IceNum":6,

    "BirdPoint":[

    {"Point1":[{"X":160,"Y":38}]},

    {"Point2":[{"X":115,"Y":38}]},

    {"Point3":[{"X":75,"Y":38}]}


    ],

    "PigPoint":[

    {"Point1":[{"X":280,"Y":38}]},

    {"Point2":[{"X":360,"Y":67}]},

    {"Point3":[{"X":390,"Y":38}]}

    ],

    "IcePoint":[

    {"Point1":[{"X":250,"Y":38}]},

    {"Point2":[{"X":250,"Y":67}]},

    {"Point3":[{"X":250,"Y":96}]},

    {"Point4":[{"X":330,"Y":38}]},

    {"Point5":[{"X":330,"Y":67}]},

    {"Point6":[{"X":355,"Y":38}]}

    ]

}

]

}

]

}




愤怒的小鸟:json如图上


#include <iostream>

using namespace std;

#include "cocos2d.h"

USING_NS_CC;

#include "ui/CocosGUI.h"



#include "SimpleAudioEngine.h"

using namespace CocosDenshion;//音乐

#include "cocostudio/CocoStudio.h"

#include "ui/CocosGUI.h"

using namespace cocos2d::ui;

#include "json/rapidjson.h"

#include "json/document.h"

#include "json/writer.h"

#include "json/stringbuffer.h"

using namespace rapidjson;



class JsonBird

{

public:

    void getJson();

    Point getPoint(int chapter,int Case,int chose,int index);

    bool getIsLock(int chapter,int Case);

    int getHighScore(int chapter,int Case);

    int getStar(int chapter,int Case);

    int getBirdsNum(int chapter,int Case);

    int getPigNum(int chapter,int Case);

    int getIceNum(int chapter,int Case);

    

    void setIsLock(int chapter,int Case,bool b);

    void setHighScore(int chapter,int Case,int high);

    void setStar(int chapter,int Case,int num);

    

    static JsonBird* getInstance();

    void baocun();

    class CGarbo // 它的唯一工作就是在析构函数中删除Singleton的实

    {

    public:

        ~CGarbo()

        {

            if (JsonBird::jsonBird)

            {

                delete JsonBird::jsonBird;

            }

        }

    };

    

private:

    JsonBird(){}

    ~JsonBird(){}

    static JsonBird *jsonBird;

    

    

    

};


#endif /* json_hpp */


#include "json.hpp"

rapidjson::Value _gameChapter;


string str[3]={"BirdPoint","PigPoint","IcePoint"};


JsonBird* JsonBird:: jsonBird=new JsonBird();

JsonBird::CGarbo cc;

string jsonPath;

rapidjson::Document doc;



JsonBird*JsonBird::getInstance()

{

    return jsonBird;

}


void JsonBird::getJson()

{

    string path = FileUtils::getInstance()->getWritablePath();

    cout<<path.c_str()<<endl;

    jsonPath = path+string("charpter2.json");

    string path2=FileUtils::getInstance()->fullPathForFilename("charpter2.json");

    

    //判断可读可写区域是否有     "charpter.json"路径

    if (!FileUtils::getInstance()->isFileExist(jsonPath) )

    {

        //获取资源

        string fileContents=FileUtils::getInstance()->getStringFromFile(path2);

        //打开或创造

        FILE *file =fopen(jsonPath.c_str(), "w");

        

        if (file)

        {

            //字符串输出

            //fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

            fputs(fileContents.c_str() , file);

            //保存 关闭

            fclose(file);

        }

    }

    string data = FileUtils::getInstance()->getStringFromFile(jsonPath);

    doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());

    if (doc.HasParseError())

    {

        log("json error!\n");

        exit(1);

    }

    if (doc.HasMember("gameCharpter"))

    {

        _gameChapter = doc["gameCharpter"];

       

    }

}


Point JsonBird:: getPoint(int chapter,int Case,int chose,int index)//chose:012index第几个点

{

    string str1=StringUtils::format("Point%d",index);

    rapidjson::Value& point=_gameChapter[chapter-1]["Case"][Case-1][str[chose].c_str()][index-1][str1.c_str()];

    rapidjson::Value& pointX=point[0]["X"];

    rapidjson::Value& pointY=point[0]["Y"];

    return Vec2(pointX.GetInt(), pointY.GetInt());

    

}

bool JsonBird::getIsLock(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["isLock"].GetBool();

}

int JsonBird::getHighScore(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["HighScore"].GetInt();

}

int JsonBird::getStar(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["Star"].GetInt();

}

int JsonBird::getBirdsNum(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["BirdsNum"].GetInt();

}

int JsonBird::getPigNum(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["PigNum"].GetInt();

}

int JsonBird::getIceNum(int chapter,int Case)

{

    return _gameChapter[chapter-1]["Case"][Case-1]["IceNum"].GetInt();

}


void JsonBird::setIsLock(int chapter,int Case,bool b)

{

    string path = FileUtils::getInstance()->getWritablePath();

    cout<<path.c_str()<<endl;

    jsonPath = path+string("charpter2.json");

    string path2=FileUtils::getInstance()->fullPathForFilename("charpter2.json");

    

    //判断可读可写区域是否有     "charpter.json"路径

    if (!FileUtils::getInstance()->isFileExist(jsonPath) )

    {

        //获取资源

        string fileContents=FileUtils::getInstance()->getStringFromFile(path2);

        //打开或创造

        FILE *file =fopen(jsonPath.c_str(), "w");

        

        if (file)

        {

            //字符串输出

            //fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

            fputs(fileContents.c_str() , file);

            //保存 关闭

            fclose(file);

        }

    }

    string data = FileUtils::getInstance()->getStringFromFile(jsonPath);

    doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());

    doc["gameCharpter"][chapter-1]["Case"][Case-1]["isLock"].SetBool(b);

    rapidjson::StringBuffer buffer;

    rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);

    doc.Accept(writer);

    FILE*file=fopen(jsonPath.c_str(), "wb");

    if (file)

    {

        fputs(buffer.GetString(), file);

        fclose(file);

    }

    

}

void JsonBird::setHighScore(int chapter,int Case,int high)

{

    string path = FileUtils::getInstance()->getWritablePath();

    cout<<path.c_str()<<endl;

    jsonPath = path+string("charpter2.json");

    string path2=FileUtils::getInstance()->fullPathForFilename("charpter2.json");

    

    //判断可读可写区域是否有     "charpter.json"路径

    if (!FileUtils::getInstance()->isFileExist(jsonPath) )

    {

        //获取资源

        string fileContents=FileUtils::getInstance()->getStringFromFile(path2);

        //打开或创造

        FILE *file =fopen(jsonPath.c_str(), "w");

        

        if (file)

        {

            //字符串输出

            //fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

            fputs(fileContents.c_str() , file);

            //保存 关闭

            fclose(file);

        }

    }

    string data = FileUtils::getInstance()->getStringFromFile(jsonPath);

    doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());    doc["gameCharpter"][chapter-1]["Case"][Case-1]["HighScore"].SetInt(high);

    rapidjson::StringBuffer buffer;

    rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);

    doc.Accept(writer);

    FILE*file=fopen(jsonPath.c_str(), "wb");

    if (file)

    {

        fputs(buffer.GetString(), file);

        fclose(file);

    }

    

}

void JsonBird::setStar(int chapter,int Case,int num)

{

    string path = FileUtils::getInstance()->getWritablePath();

    cout<<path.c_str()<<endl;

    jsonPath = path+string("charpter2.json");

    string path2=FileUtils::getInstance()->fullPathForFilename("charpter2.json");

    

    //判断可读可写区域是否有     "charpter.json"路径

    if (!FileUtils::getInstance()->isFileExist(jsonPath) )

    {

        //获取资源

        string fileContents=FileUtils::getInstance()->getStringFromFile(path2);

        //打开或创造

        FILE *file =fopen(jsonPath.c_str(), "w");

        

        if (file)

        {

            //字符串输出

            //fputs(const char *str, FILE *stream) 把字符串写入到指定的流 stream 中,但不包括空字符。

            fputs(fileContents.c_str() , file);

            //保存 关闭

            fclose(file);

        }

    }

    string data = FileUtils::getInstance()->getStringFromFile(jsonPath);

    doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());    doc["gameCharpter"][chapter-1]["Case"][Case-1]["Star"].SetInt(num);

    rapidjson::StringBuffer buffer;

    rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);

    doc.Accept(writer);

    FILE*file=fopen(jsonPath.c_str(), "wb");

    if (file)

    {

        fputs(buffer.GetString(), file);

        fclose(file);

    }

    

}


void JsonBird:: baocun()

{

    rapidjson::StringBuffer buffer;

    rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);

    doc.Accept(writer);

    FILE*file=fopen(jsonPath.c_str(), "wb");

    if (file)

    {

        fputs(buffer.GetString(), file);

        fclose(file);

    }

}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值