[英]How to look through an array that is inside a .json using rapidjson? (cocos2d-x)
我認為問題是具體的,我想遍歷一個形式為.json的數組:
{ "N" : 5, "Rotacion" : 42, "Igual" : 20, "Inverso" : 0, "RotacionE" : 47, "Espejo" : 22, "Puntuacion" : 0, "_id" : "563b7b4756ab632f47fe6d7f" , "Lados" : [], "Camino" : [ 6, 5, 4, 21, 22, 7, 2, 3, 20, 23, 8, 1, 18, 19, 24, 9, 0, 17, 16, 15, 10, 11, 12, 13, 14 ], "__v" : 0 }
我搜索了一些教程,他們告訴我要做以下事情:
const Value& a = document["a"];
assert(a.IsArray());
for (SizeType i = 0; i < a.Size(); i++) // Uses SizeType instead of size_t
printf("a[%d] = %d\n", i, a[i].GetInt());
這個例子的問題是,在編譯時我得到以下錯誤:
/home/jmuniz/code/Cocos2d-x/interface/Classes/HelloWorldScene.cpp:84:7: error: reference to ‘Value’ is ambiguous
const Value& a = d["Camino"];
和
/home/jmuniz/code/Cocos2d-x/interface/cocos2d/cocos/base/CCValue.h:54:14: note: candidates are: class cocos2d::Value
class CC_DLL Value
^
In file included from /home/jmuniz/code/Cocos2d-x/interface/Classes/HelloWorldScene.cpp:4:0:
/home/jmuniz/Dev/rapidjson-master/include/rapidjson/document.h:1758:31: note: typedef class rapidjson::GenericValue<rapidjson::UTF8<> > rapidjson::Value
typedef GenericValue<UTF8<> > Value;
^
/home/jmuniz/code/Cocos2d-x/interface/Classes/HelloWorldScene.cpp:84:7: error: ‘Value’ does not name a type
const Value& a = d["Camino"];
在這里我放了一段打開.json的代碼,這樣你就可以知道我在做什么了
FILE* fp = fopen("/home/jmuniz/code/Cocos2d-x/interface/Resources/res/puzzles(copia).json", "r"); // non-Windows use "r"
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);
fclose(fp);
請問我需要知道錯誤發生的原因? 或者至少告訴我如何訪問陣列進行打印然后操作
這是因為有兩種類型具有相同名稱的Value
。
要解決歧義,只需使用rapidjson::Value
,或者輸入新名稱。
string josn="{ \"N\" : 5, \"Rotacion\" : 42, \"Igual\" : 20, \"Inverso\" : 0, \"RotacionE\" : 47, \"Espejo\" : 22, \"Puntuacion\" : 0, \"_id\" : \"563b7b4756ab632f47fe6d7f\" , \"Lados\" : [], \"Camino\" : [ 6, 5, 4, 21, 22, 7, 2, 3, 20, 23, 8, 1, 18, 19, 24, 9, 0, 17, 16, 15, 10, 11, 12, 13, 14 ], \"__v\" : 0 }";
rapidjson::Document doc;
if (!doc.Parse<0>(josn.c_str()).HasParseError()) {
const rapidjson::Value& myArray=doc["Camino"];
vector<int> Camino;
if (myArray.GetType()==rapidjson::kArrayType) {
for (int i=0; i<myArray.Size(); i++) {
Camino.push_back(myArray[i].GetInt());
}
for (auto it=Camino.begin(); it!=Camino.end(); it++) {
printf("%d\n",*it);
}
}
}else{
printf("1 error parsing the json %zu\n",doc.GetErrorOffset());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.