I have a String containing json
data in text/plain
format which was returned by a web response. I'm trying to get a specific value from mail key through Google's Gson library. However, after following some examples in google I still can't get the value for mail. Instead, it returns all data contained in String.
public class EmailData {
public String mail;
public String getMail() {
return mail;
}
}
The response returned via a link has the ff (text/plain
). This is what's shown on browser and console.
{
"search": {
"entry": [
{
"dn": "uid=C12345678,c=ph,ou=pages,o=website.com",
"attribute": [
{
"name": "mail",
"value": [
"myemail123@website.com"
]
},
{
"name": "employeecountrycode",
"value": [
"818"
]
},
{
"name": "dept",
"value": [
"ABC"
]
}
],
"return": {
"code": 0,
"message": "Success",
"count": 1
}
}
]
}
}
I want to be able to get just the value for mail
which is myemail123@website.com
So what I tried is this.
public class Test {
public static void main(String[] args) {
String linkReturningJsonTextPlain = "http://....";
try (WebClient webClient = new WebClient();){
Page page = webClient.getPage(linkReturningJsonTextPlain);
WebResponse response = page.getWebResponse();
String jsonString = response.getContentAsString();
Gson gson = new Gson();
EmailData emailData = gson.fromJson(jsonString, EmailData.class);
System.out.println(emailData.getMail());
} catch (Exception e) {
e.printStackTrace();
}
}
I get all the data contained in jsonString
instead of just myemail123@website.com.
What am I doing wrong? I followed all examples. I get text/plain when I print response.getContentType()
My last option now is to do the manual splitting and substring(ing) of the json String.
Thanks.