如何获取JsonCPP值作为字符串? [英] How to get JsonCPP values as strings?

查看:459
本文介绍了如何获取JsonCPP值作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JsonCpp 解析json数据.我真的不需要了解数据,只需要打印出一些属性及其值即可.这很难做到.首先,我需要知道值是什么类型,然后获取值,然后再次将其转换为字符串!有样式的作家,但我不想使用它,因为它在结尾处附加了一些CRLF.

I am parsing json data using JsonCpp. I don't really need to understand the data, i just need to print out some properties and their values out. It somehow is hard to do. First I need to know what type the value is and then get the value and then convert it to string again! There is a styled writer but I don't want to use it as it appends some CRLF at the end.

我做这样的事情

CJsonHelper::getUInt(Json::Value &root, std::string key){
    return root.get(key, 0-1).isInt() ? root.get(key, 0-1).asUInt() : 0-1;
}

我可以只编写一个函数来获取该函数的所有属性,而实际上并不关心类型等吗?

Could I just write a single function to get all the properties with just that function which doesn't really care about the types etc?

推荐答案

您可以轻松创建自己的作家,该作家可以做任何您想做的事情.看一下StyledWriter的代码.您不喜欢的CRLF来自StyledWriter::Write.您可以创建自己的不具有CRLF的writer类.

You can trivially create your own writer that does whatever you want. Have look at the code for StyledWriter. The CRLF's you don't like come from StyledWriter::Write. You can create your own writer class that doesn't have those CRLF's.

如果您不介意修改库,请更改编写器,以使writeValue之类的功能受到保护,而不是私有的.然后,您可以使用不同的Write函数从StyledWriter派生您自己的类,并且仍然使用各种write*函数进行转换的详细信息.

If you don't mind modifying the library, change the writer so that functions like writeValue are protected rather than private. Then you can derive your own class from StyledWriter with a different Write function and still use the various write* functions to do the details of the conversion.

这篇关于如何获取JsonCPP值作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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