Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Deserialize json object
Deserialize json object [message #40759] Fri, 13 September 2013 13:11 Go to next message
andreicpp is currently offline  andreicpp
Messages: 2
Registered: September 2013
Location: Romania
Junior Member
Hello,

I am receiving via httprequest a json object which I put in a String.

This is how it looks:

sResponse={"response":{"username":"andrei","role":"user ","firstName":"andrei","lastName":"nume","email":"andrei@gmail.com","id":2,"password":"1234"},"status":{"description":[], "status":"OK"}}

How can I deserialize it and put it in an object of class

class Response {
Username usr;
Response response;
}


I tried implementing jsonize method in this class Response, and than use loadfromjson, but no positive results.

Can someone help me?
Thank you

Re: Deserialize json object [message #40825 is a reply to message #40759] Sat, 21 September 2013 19:04 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
There is no direct way how to do this "automatically". Usually, there is no need to store it in C++ structure too.

I usually do something like

Value r = ParseJSON(json_text)["response"];
r["username"]
r["email"]

Note that things are aranged so that failed ParseJSON returns empty Value and trying to access key in empty Value or missing key results in another empty Value (so the code above does not crash if json is invalid or keys are missing, just results in blanks..)

Mirek
Re: Deserialize json object [message #40849 is a reply to message #40825] Mon, 23 September 2013 10:23 Go to previous message
andreicpp is currently offline  andreicpp
Messages: 2
Registered: September 2013
Location: Romania
Junior Member
Thanks a lot
I wrote a method included in the class:

void HTTPResponseUser::LoadFromJson(Value json){
m_User.LoadFromJson(json["response"]);
m_status.LoadFromJson(json["status"]);

}
Previous Topic: Build options
Next Topic: SortedIndex and Less
Goto Forum:
  


Current Time: Thu Apr 25 13:49:58 CEST 2024

Total time taken to generate the page: 0.03549 seconds