Arnab
Arnab

Reputation: 63

Convert String to JSON in Java REST

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 !!

Upvotes: 0

Views: 180

Answers (2)

TR1
TR1

Reputation: 323

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
}

Upvotes: 0

StackFlowed
StackFlowed

Reputation: 6816

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
}

Upvotes: 2

Related Questions