gpt4 book ai didi

cocos2d-x - RapidJson : How to get all Key_names from a JSON? (cocos2dx)

转载 作者:行者123 更新时间:2023-12-01 22:19:19 24 4
gpt4 key购买 nike

从 Json 字符串(或文件)中,我想在不提前知道键的情况下收集键/值对。假设我有这个 Json:

{ "a":"1","b":"2","c":"3" }

我想收集所有关键字符串 "a"、 "b"、 "c"、 "d"及其各自的值。顺便说一句:我在 Cocos2dX 3.3 中使用rapidjson集成。有什么想法吗?

我现在要做的是使用:

rapidjson::Document JSON; 

//..... collecting the JSON .... then

for (rapidjson::Value::MemberIterator M=JSON.MemberonBegin(); M!=JSON.MemberonEnd(); M++)
{
//..... I have access to M->name and M->value here
//..... but I don't know how to convert them to std::string or const char*
}

但我坚持这一点。

最佳答案

我刚刚发现 rapidjson::Value::MemberIterator 中有函数。下面是一个从 Json 文档中枚举键/对的示例。此示例仅记录根 key 。您将需要额外的工作来检索子 key

const char *jsonbuf = "{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}";

rapidjson::Document JSON;
rapidjson::Value::MemberIterator M;
const char *key,*value;

JSON.Parse<0>(jsonbuf);

if (JSON.HasParseError())
{
CCLOG("Json has errors!!!");
return;
}

for (M=JSON.MemberonBegin(); M!=JSON.MemberonEnd(); M++)
{
key = M->name.GetString();
value = M->value.GetString();

if (key!=NULL && value!=NULL)
{
CCLOG("%s = %s", key,value);
}
}

关于cocos2d-x - RapidJson : How to get all Key_names from a JSON? (cocos2dx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com