I am trying to parse json and have it display in a visualforce page.
json:
{
"type": "success",
"value": {
"id": 151,
"joke": "Chuck Norris doesn't step on toes. Chuck Norris steps on necks.",
"categories": []
}
}
controller:
public class chuckNorris {
public List<ValueWrapper> value {get;set;}
public class ValueWrapper {
public String joke;
public Integer id;
}
public void deserialize () {
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://api.icndb.com/jokes/random');
req.setMethod('GET');
req.setHeader('Content-type', 'application/json');
HttpResponse res = h.send(req);
String jsonString = res.getBody();
chuckNorris chuck = (chuckNorris)JSON.deserialize(jsonString, chuckNorris.class);
system.debug(chuck.value[0].joke);
system.debug(chuck.value[0].id);
}
}
I believe I am using the wrapper class correctly. If I am, how would I display the joke on a visualforce page? If the wrapper class isn't correct what needs to be changed?
Best Answer
The right apex based on the JSON is as below
Visualforce page
Use merge field like below
{!chuck.value.joke}
Vf code is below: