Thread: [Cppcms-users] Is there any way to get a list of key values from a JSON object?
Brought to you by:
artyom-beilis
From: doug l. <bi...@gm...> - 2012-05-10 16:59:00
|
Hi -- if I had this json object: { "service": { "api": "http", "port": 3001 }, "http": { "script_names": [ "/imageservice" ] }, "imageservice": { "secret": "sup3rs3cr3t", "storage": "tmp", "types": { "test-type-1": { "on_save": [ ".JPEG" ], "versions": { "big": [ "300x1000>" ], "thumb": [ "75x75#" ], "thumb-png": [ "75x75#", ".PNG" ] } }, "test-type-2": { "on_save": [ "600x150!" ], "versions": { "gallery-jpeg": [ "50%", ".JPEG" ], "gallery-png": [ "50%", ".PNG" ] } } } } } Would it be possible for me to query the JSON value object to find out that there are two "type" values in the "imageservice" bit, and that those types had keys of "test-type-1" and "test-type-2"? I've been trying & failing to achieve this. Thanks for any & all help, Doug. |
From: Daniel V. <chi...@gm...> - 2012-05-10 17:49:39
|
using cppcms; try { json::value v; ... // test if object "types" exist json::object& o types = v.at("types").object(); // note that "o" is a std::map for ( json::object::iterator i = o.begin; i != o.end(); ++i) { // i->first contains key name // i->second contains mapped value ... } } catch(json::bad_value_cast const& e) { //error handling } Reference: http://cppcms.com/cppcms_ref/latest/classcppcms_1_1json_1_1value.html#aa42bb35c356312bdee5a37bc0350264c http://cppcms.com/cppcms_ref/latest/classcppcms_1_1json_1_1value.html#ac8bcaa686b3ec1ad0cc22d663bce4566 http://cppcms.com/cppcms_ref/latest/namespacecppcms_1_1json.html#a4849268540ccdea30cfa408d6706d0c9 > Would it be possible for me to query the JSON value object to find out > that there are two "type" values in the "imageservice" bit, and that > those types had keys of "test-type-1" and "test-type-2"? > I've been trying & failing to achieve this. > Thanks for any & all help, > Doug. |
From: kpeo <sla...@ya...> - 2012-05-10 18:05:17
Attachments:
main.cpp
|
<div>Hello,</div><div> </div><div>As explained in message before, use iterators over json object:</div><div> </div><div> cppcms::json::object ob=settings().get<cppcms::json::object>("imageservice.types");<br /><br /> for(cppcms::json::object::const_iterator p=ob.begin();p!=ob.end();++p) {<br /> if(p->second.type() == cppcms::json::is_object){<br /> std::cout << "key [" << p->first << "]: " << p->second << std::endl;<br /> } <br /> } </div><div> </div><div>Full code (for *nix) attached, compile: "g++ -lcppcms main.cpp -o test"</div><div> </div><div>Also, for some detail, see article:</div><div> </div><div>http://cppcms.com/wikipp/en/page/cppcms_1x_json</div><div> </div><div>10.05.2012, 20:58, "doug livesey" <bi...@gm...>:</div><blockquote type="cite">Hi -- if I had this json object:<div><div>{</div><div> "service": {</div><div> "api": "http",</div><div> "port": 3001</div><div> },</div><div> "http": {</div><div> "script_names": [ "/imageservice" ]</div><div> },</div><div> "imageservice": {</div><div> "secret": "sup3rs3cr3t",</div><div> "storage": "tmp",</div><div> "types": {</div><div> "test-type-1": {</div><div> "on_save": [ ".JPEG" ],</div><div> "versions": {</div><div> "big": [ "300x1000>" ],</div><div> "thumb": [ "75x75#" ],</div><div> "thumb-png": [ "75x75#", ".PNG" ]</div><div> }</div><div> },</div><div> "test-type-2": {</div><div> "on_save": [ "600x150!" ],</div><div> "versions": {</div><div> "gallery-jpeg": [ "50%", ".JPEG" ],</div><div> "gallery-png": [ "50%", ".PNG" ]</div><div> }</div><div> }</div><div> }</div><div> }</div><div>}</div></div><div>Would it be possible for me to query the JSON value object to find out that there are two "type" values in the "imageservice" bit, and that those types had keys of "test-type-1" and "test-type-2"?</div><div>I've been trying & failing to achieve this.</div><div>Thanks for any & all help,</div><div> Doug.</div><p>------------------------------------------------------------------------------<br />Live Security Virtual Conference<br />Exclusive live event will cover all the ways today's security and <br />threat landscape has changed and how IT managers can respond. Discussions <br />will include endpoint security, mobile security and the latest in malware <br />threats. <a href="http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/">http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/</a></p><p>_______________________________________________<br />Cppcms-users mailing list<br /><a href="mailto:Cpp...@li...">Cpp...@li...</a><br /><a href="https://lists.sourceforge.net/lists/listinfo/cppcms-users">https://lists.sourceforge.net/lists/listinfo/cppcms-users</a></p></blockquote> |
From: doug l. <bi...@gm...> - 2012-05-10 18:49:20
|
That did the trick, thankyou very much! On 10 May 2012 19:05, kpeo <sla...@ya...> wrote: > Hello, > > As explained in message before, use iterators over json object: > > cppcms::json::object > ob=settings().get<cppcms::json::object>("imageservice.types"); > > for(cppcms::json::object::const_iterator > p=ob.begin();p!=ob.end();++p) { > if(p->second.type() == cppcms::json::is_object){ > std::cout << "key [" << p->first << "]: " << > p->second << std::endl; > } > } > > Full code (for *nix) attached, compile: "g++ -lcppcms main.cpp -o test" > > Also, for some detail, see article: > > http://cppcms.com/wikipp/en/page/cppcms_1x_json > > 10.05.2012, 20:58, "doug livesey" <bi...@gm...>: > > Hi -- if I had this json object: > { > "service": { > "api": "http", > "port": 3001 > }, > "http": { > "script_names": [ "/imageservice" ] > }, > "imageservice": { > "secret": "sup3rs3cr3t", > "storage": "tmp", > "types": { > "test-type-1": { > "on_save": [ ".JPEG" ], > "versions": { > "big": [ "300x1000>" ], > "thumb": [ "75x75#" ], > "thumb-png": [ "75x75#", ".PNG" ] > } > }, > "test-type-2": { > "on_save": [ "600x150!" ], > "versions": { > "gallery-jpeg": [ "50%", ".JPEG" ], > "gallery-png": [ "50%", ".PNG" ] > } > } > } > } > } > Would it be possible for me to query the JSON value object to find out > that there are two "type" values in the "imageservice" bit, and that those > types had keys of "test-type-1" and "test-type-2"? > I've been trying & failing to achieve this. > Thanks for any & all help, > Doug. > > > ------------------------------------------------------------------------------ > > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Cppcms-users mailing list > Cpp...@li... > https://lists.sourceforge.net/lists/listinfo/cppcms-users > > |