在c中将json值转换为int

新手上路,请多包涵

我正在使用 C++ 读取 json 值

Json::Reader reader

并且该值存储在 Json::Value root

这个根包含“age”和“id”,我想将 root[“age”] 转换为 int。

我尝试使用 .str() 将其转换为字符串,但无法获得。

有什么建议吗?

原文由 rocx 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
1 个回答

jsoncpp 他们在 Json::Value 对象上提供辅助方法。您只需调用 asInt() 方法对值进行转换即可。

 int ageAsInt = root["age"].asInt()

原文由 Mark Loeser 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题