2

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.

Velaniv Jr
  • 164
  • 9
heisenberg
  • 1,784
  • 4
  • 33
  • 62

2 Answers2

3

In your json format you cannot convert EmailData class back. The json format should be {"mail":"mail@example.com"}

Consider the code:

EmailData class.

package gson;

public class EmailData {

    private String mail;

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    @Override
    public String toString() {
        return "EmailData{" +
               "mail='" + mail + '\'' +
               '}';
    }
}

Test class for showing how Gson converts

package gson;

import com.google.gson.Gson;

public class Test {

    public static void main(String[] args) {

        Gson gson = new Gson();

        EmailData emailData = new EmailData();
        emailData.setMail("mail@example.com");

        String toJson = gson.toJson(emailData);

        System.out.println("toJson = " + toJson);

        EmailData fromJson = gson.fromJson(toJson, EmailData.class);
        System.out.println("fromJson = " + fromJson);
    }
}

In this example I am converting EmailData class to json and back. For solving your problem I recommend you to use whether Regular expression or Json for extraction email like shown in this post How to parse JSON and the create an EmailData class manually via constructor.

Velaniv Jr
  • 164
  • 9
0

I'm writing this answer as reference to those who might encounter problems pulling record from a bunch of keys with array values contained in json response.

Given the json data in text format above, this is how I got the value from mail element.

JsonArray attribute_Array = jsonObject.getAsJsonObject("search").
            getAsJsonArray("entry").get(0).getAsJsonObject().getAsJsonArray("attribute");
String email = "not found";
for (JsonElement e : attribute_Array) {
    String key = e.getAsJsonObject().get("name").toString().replace("\"", "");
    if(key.equals("mail")){
        email = e.getAsJsonObject().get("value").toString();
    }
}

email, whose initial value is "not found" will be replaced by whatever value the json element mail contains.

As you can see, both entry and attribute was passed as argument to getAsJsonArray() because both has arrays of values.

I'm sure this will help others who might get confused in pulling values from nested arrays within Json.

Thanks to @Velaniv Jr, the link you provided helped.

heisenberg
  • 1,784
  • 4
  • 33
  • 62