json反序列化
#include
"cocos-ext.h"
USING_NS_CC ;
USING_NS_CC_EXT ;
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 ());
}
}
}
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"
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);
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. 引起歧义的; 模棱两可的, 含糊不清的