如何使用rapidjson浏览.json中的数组?(椰子2d-x)
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] = %dn", 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("%dn",*it);
}
}
}else{
printf("1 error parsing the json %zun",doc.GetErrorOffset());
}
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 在尝试执行触摸和保持(子弹发射)时卡住了?-椰子2d-x
- 圆角矩形在椰子2d-x与贝塞尔
- 如何使用rapidjson浏览.json中的数组?(椰子2d-x)
- 椰子2d-x中的EXC_BAD_ACCESS