json反序列化和反序列化

json反序列化
#include "cocos-ext.h"
USING_NS_CC
;
USING_NS_CC_EXT ;
using namespace rapidjson ;
const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}" ;
   
   
Document doc;
    doc.
Parse < 0 >(json);
   
if (doc. HasParseError ()) {
       
CCLOG ( "GetParseError %s" ,doc. GetParseError ());
    }
else {
   
       
if (doc. HasMember ( "hello" )) {
           
std :: string hello = doc[ "hello" ]. GetString ();
           
CCLOG ( " %s" ,hello. c_str ());
        }
   
   
       
if (doc. HasMember ( "t" )) {
           
bool t = doc[ "t" ]. GetBool ();
           
CCLOG ( " %s" ,t ? "true" : "false" );
        }
       
       
       
if (doc. HasMember ( "f" )) {
           
bool f = doc[ "f" ]. IsBool ();
           
CCLOG ( " %s" ,f ? "true" : "false" );
        }
       
       
if (doc. HasMember ( "n" )) {
           
bool n = doc[ "n" ]. IsNull ();
           
CCLOG ( " %s" ,n ? "is null" : "not null" );
        }
   
       
if (doc. HasMember ( "i" )) {
           
int i = doc[ "i" ]. GetInt ();
           
CCLOG ( " %d" ,i );
        }
   
       
if (doc. HasMember ( "pi" )) {
           
double pi = doc[ "pi" ]. GetDouble ();
           
CCLOG ( " %lf" ,pi );
        }

       
rapidjson :: Value   &a = doc[ "a" ];
       
if (a. IsArray ()) {
           
for ( int i = 0 ; i<a. Size (); i++) {
                
CCLOG ( " a[%d] = %d" ,i,a[ i ]. GetInt ());
            }
        }
   
   
    }
   




json序列化
#include "cocos-ext.h"
USING_NS_CC_EXT
;
using namespace rapidjson ;

USING_NS_CC ;


//#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
   //json 序列化
   
Document doc;
    doc.
SetObject ();
   
Document :: AllocatorType &allocator = doc. GetAllocator ();
   
   
// 添加单个字符串信息
//    doc.AddMember(const Ch *name, T value, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> &allocator);
   
    doc.
AddMember ( "tittle" , "stu_info" ,allocator);
   
   
rapidjson :: Value   stuArr( kArrayType );
   
   
   
rapidjson :: Value   stu1( kObjectType );
   
    stu1.
AddMember ( "id" , 1 ,allocator);
    stu1.
AddMember ( "name" , "Peter" ,allocator);
    stu1.
AddMember ( "sex" , "male" ,allocator);
    stu1.
AddMember ( "grade" , 98.5 ,allocator);
   
//    stuArr.PushBack(T value, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> &allocator);
   
    stuArr.
PushBack (stu1,allocator);
   
   
   
   
rapidjson :: Value   stu2( kObjectType );
   
    stu2.
AddMember ( "id" , 2 ,allocator);
    stu2.
AddMember ( "name" , "Marry" ,allocator);
    stu2.
AddMember ( "sex" , "female" ,allocator);
    stu2.
AddMember ( "grade" , 90.3 ,allocator);
    stuArr.
PushBack (stu2,allocator);
   
   
   
// 添加数组
    doc.
AddMember ( "students" , stuArr,allocator);
   
   
// 转化成字符串
   
StringBuffer buffer;
   
   
Writer < StringBuffer > writer(buffer);
   
//    doc.Accept(Handler &handler);
    doc.
Accept (writer);
   
const char * json = buffer. GetString ();
   
CCLOG ( " %s" ,json);
   
输出:
 {"tittle":"stu_info","students":[{"id":1,"name":"Peter","sex":"male","grade":98.5},{"id":2,"name":"Marry","sex":"female","grade":90.3}]}



还可以参考 
http://cn.cocos2d-x.org/tutorial/show?id=1203   Cocos2d-x 3.0 Json用法

ambiguous/æm'bɪɡjuəs/ adj. 引起歧义的; 模棱两可的, 含糊不清的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值