如何在c ++中打印JSON值 [英] How to print JSON value in c++

查看:922
本文介绍了如何在c ++中打印JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想用c ++询问Json。

如何在c ++中打印Json值,



假设我有这个Json:

 {
nameS Jason
ageS 16
}





如何打印他是杰森或16岁。



我已经尝试过这些:



  void  printmyJson(Json ::Value¶ms)
{
....
Json :: Value root;
std :: string name;
std :: string age;
std :: string mName;
for (Json :: ValueIterator it = params.begin(); it!= params.end(); ++ it)
{
mName = it.memberName();
name = root.get(mName, 0 )。asString.c_str();
}

printf( 他是%s,名字\\\
);
printf( 他是%s,mName);
....
}





但它总是输出:

他是

他的名字



所以,变量名称 NULL 而不是它的值(Jason)。变量 mName 用Json字符串填充而不是(我想要的)它的值(Jason)。



有人可以帮我这个?



非常感谢

解决方案

我假设您使用的是JsonCPP



如果你看看这个问题的答案,你会看到如何迭代json树。



http://stackoverflow.com/questions/4800605/jsoncpp-iterate-thru-all-the-objects [ ^ ]



此函数

  void  CDriverConfigurator :: PrintJSONValue(Json :: Value& val)



打印JSON值。



调试器是检查值的最佳方法获得

Hello,

I wanna ask about Json in c++.
How can I print Json value in c++,

Let's say that I have this Json :

{
   "nameS" : "Jason",
   "ageS"  : "16"
}



How can I print "He's Jason" or "16 years old".

I already tried these :

void printmyJson(Json::Value ¶ms)
{
....
Json::Value root;
std::string name;
std::string age;
std::string mName;
for(Json::ValueIterator it = params.begin(); it!= params.end(); ++it)
{
 mName = it.memberName();
 name = root.get(mName,0).asString.c_str();
}

printf("He's %s",name\n);
printf("He is %s",mName);
....
}



But it always output this :
"He's "
"He's name"

So, the variable name is NULL instead of it's value (Jason). And variable mName is filled with Json string instead of(which I wanted)it's value (Jason).

Can someone help me on this?

many thanks

解决方案

I assume you are using JsonCPP

If you look at the answer to this question you will see how to iterate the json tree.

http://stackoverflow.com/questions/4800605/jsoncpp-iterate-thru-all-the-objects[^]

This function

void CDriverConfigurator::PrintJSONValue( Json::Value &val )


prints the JSON values.

The debugger is the best way to check the values you are getting.


这篇关于如何在c ++中打印JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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