Click here to Skip to main content
15,915,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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

Let's say that I have this Json :
JavaScript
{
   "nameS" : "Jason",
   "ageS"  : "16"
}


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

I already tried these :

C++
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
Posted
Updated 5-Sep-13 18:28pm
v2

1 solution

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
C#
void CDriverConfigurator::PrintJSONValue( Json::Value &val )

prints the JSON values.

The debugger is the best way to check the values you are getting.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900