JSONcpp通过对象中的列表进行迭代 [英] JSONcpp iterate through a list within the object

查看:991
本文介绍了JSONcpp通过对象中的列表进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json对象有一个列表,我想迭代通过列表的元素
我看到下面的帖子 JSonCPP,遍历所有的对象,但这对我没有工作

My json object has a list in it and I would like to iterate through the elements of the list I saw the following post JSonCPP, iterate thru all the objects , but this did not work for me

我的json对象:

{
         "name": ["str1",str2" ... ]
}

我拥有的代码不起作用

Json::Value names= (*json)["name"];
for( Json::ValueIterator itr = names.begin() ; itr != names.end() ; itr++ ) {
    string name =  *itr.asString();}

我收到以下错误

cannot convert from 'Json::Value' to 'std::basic_string<_Elem,_Traits,_Ax>



我确定元素是字符串,因为调用 string name = names =(* json)[name] [0] .asString()正在工作

推荐答案

p>我看不到你怎么可以得到一个不能转换错误从你的代码,但我确实认为我看到的问题。

I don't see how you can get a cannot convert error from your code, but I do think I see the problem.

Json :: ValueIterator 是一个迭代器;


  • itr.asString() tries to access the asString method of the iterator, and the iterator doesn't have an asString method.
  • *itr.asString() tries to access the asString method of the iterator then dereference the result (because of order of operations; . has a higher precedence than *), and the iterator doesn't have an asString method.
  • (*itr).asString() (using parentheses to clarify precedence) or itr->asString() (using the -> operator, the preferred approach) should work.

这篇关于JSONcpp通过对象中的列表进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆