0

Below is my JSON:

{
  "time":{
    "date":{
      "year":2017,
      "month":3,
      "day":12
     },
    "time":{
      "hour":10,
      "minute":42,
      "second":42,
      "nano":810000000
     }
   },
"name":"Jon",
"message":{
"product":"orange"
"price":2000
}
}

'time' field has a nested 'time' field. How can I parse this using jackson to java object. Can anyone please tell me the correct method?

4
  • Parse to a POJO or a Map? You'll easily find 2000+ answers for both here on SO (here or here) or in a source of your choice. Commented Mar 28, 2019 at 8:56
  • I found a lot of answers but i did not find any to include "same name nested field" that converts JSON to POJO Commented Mar 28, 2019 at 8:58
  • Ok, sorry, i didn't understand you only want the inner time property. You can just parse everything to a JsonNode, get the inner Property as JsonNode and parse that to your POJO (see my answer). Commented Mar 28, 2019 at 9:20
  • 1
    In case you fall in other JSON structure you can always generate POJO model using online tools like it is described in this question: Array of JSON Object to Java POJO Commented Mar 28, 2019 at 9:45

3 Answers 3

2

You can create classes like these:

class JavaObject {
    private TimeObject time;
    private String name;
    //other fields
    //getters and setters
}

class TimeObject {
    private Date date;
    private Time time;
    //getters and setters
}

class Date {
    private int year;
    private int month;
    private int day;
    //getters and setters
}

class Time {
    private int hour;
    private int minute;
    private int second;
    private long nano;
    //getters and setters
}

Once done, you can use Jackson to deserialize the json String into JavaObject object, e.g.:

ObjectMapper objectMapper = new ObjectMapper();
JavaObject javaObject = objectMapper.readValue("{\n" + 
        "  \"time\":{\n" + 
        "    \"date\":{\n" + 
        "      \"year\":2017,\n" + 
        "      \"month\":3,\n" + 
        "      \"day\":12\n" + 
        "     },\n" + 
        "    \"time\":{\n" + 
        "      \"hour\":10,\n" + 
        "      \"minute\":42,\n" + 
        "      \"second\":42,\n" + 
        "      \"nano\":810000000\n" + 
        "     }\n" + 
        "   },\n" + 
        "\"name\":\"Jon\"}", JavaObject.class);
System.out.println(javaObject);
Sign up to request clarification or add additional context in comments.

2 Comments

If they only need the data from the inner time prop, this seems like a lot of overhead. Or did i get the requirements wrong?
Thanks so much. Can you help me with one more thing. Sometimes the time field comes with hour, minute, second separately and sometimes it comes in string like "10:42:42:810000000" directly. In that case how can i create Time class?
1

If you only need the inner time object, you could do this in a quick way:

// your POJO class
// (public fields sould be private with getter & setter, of course)

public class Pojo {
    public int hour;
    public int minute;
    public int second;
    public long nano;

    @Override
    public String toString() {
        return hour + ":" + minute + ":" + second + ":" + nano;
    }
}

And then:

//your json string
String jsonString = "{\"time\":{\"date\":{\"year\":2017,\"month\":3,\"day\":12},"
                + "\"time\":{\"hour\":10,\"minute\":42,\"second\":42,\"nano\":810000000}},"
                + "\"name\":\"Jon\",\"message\":{\"product\":\"orange\",\"price\":2000}}";

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonRoot = mapper.readTree(jsonString); //parse string to JsonNode
Pojo pojo = mapper.treeToValue(jsonRoot.at("/time/time"), Pojo.class); //create Pojo instance from inner time object
System.out.println(pojo); //see if it worked

This prints:

10:42:42:810000000

2 Comments

Thanks so much. Can you help me with one more thing. Sometimes the time field comes with hour, minute, second separately and sometimes it comes in string like "10:42:42:810000000" directly. In that case how can i parse it?
@user812142 I don't really understand your question. The 10:42:42:810000000 is just a string that is constructed in the toString() method of the POJO. I only added it to show you an output that proves the data is there. If your Json doesn't contain minute or hour, those values will not be set (and left to default, so 0 in this case, but you could change that).
0

Use Jackson library

ObjectMapper jsonMapper= new ObjectMapper();
YourCorrespondingObject object = jsonMapper.readValue("your json as string...", YourCorrespondingObject.class);

it's easier first to build your object, populate it, and convert to string, to make sure it equals your existing string, like:

String jsonInString = mapper.writeValueAsString(object);

Comments

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.