0

I have a JSON object, generated from a REST service, that looks like this:

  {
    "name": "mark",
    "other_details": "{age:34, gender:male}"
  }

In the "other_details" param, the value has to be converted to a JSON object, which ultimately should look like:

{
    "name": "mark",
    "other_details": "{
                          "age":"34", 
                          "gender":"male"
                      }"
}

My POJO looks like:

class Profile{
          String name;
          String other_details;
         //getters and setters
}

I need some help regarding converting the value of the "other_details" param(which is a string) into a JSON. I did try to use Jackson, but it was of no use.

any ideas, how should I proceed !!

1
  • In your Java code "other_details" is represented as a String, but in your JSON it is mapped as an object. Commented Nov 16, 2015 at 20:27

2 Answers 2

2

You pojo should look like

public class Profile{
   String name;
   OtherDetailsClass other_details;
   //getters and setters
}

Other Details Class should look like

public class OtherDetailsClass {
    String age;
    String gender;
    //getters and setters
}
Sign up to request clarification or add additional context in comments.

2 Comments

I did try this, but I guess, its because of the way the other_details value is stored. The other_details value is fetched from the database, where that column is of String type and is of format {age:34, gender:male} . Is it possible to parse and convert it to JSON format in POJO.
use @JsonProperty(value = "other_details") for mapping.
0

Try adding a POJO for other_details.

class Profile{
          String name;
          OtherDetails other_details;
         //getters and setters
}

class OtherDetails{
          String name;
          String gender;
         //getters and setters
}

1 Comment

I did try this, but I guess, its because of the way the other_details value is stored. The other_details value is fetched from the database, where that column is of String type and is of format {age:34, gender:male} . Is it possible to parse and convert it to JSON format in POJO ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.