-4

I have this json data, i want to get "fine" -> "start_time" value. I m using Gson, i just want to know the easy way.

{
  "result_index": 0,
  "results": [
{
  "final": true,
  "alternatives": [
    {
      "confidence": 0.715,
      "transcript": "hello how are you holds the medically I hope you are fine "
    }
  ],
  "keywords_result": {
    "fine": [
      {
        "normalized_text": "fine",
        "start_time": 5.85,
        "end_time": 6.27,
        "confidence": 0.918
      }
    ]
  }
}
]
}

1 Answers1

0

Hello there dear friend,

  • You may use Jackson for this purpose. Assuming that is a maven project, you can add dependency with:

    com.fasterxml.jackson.core jackson-databind 2.6.3

And then you can use jackson library and parse json string to your relevant object , in this case you define your pojos like:

public class Staff {
    private int result_index;
    private List<Result> results;
    //getters & setters
}
public class Result {
    private boolean final;
    private List<Alternative> alternatives;
    private KeywordsResult keywords_result;
    //getters & setters

}

public class Alternative{
    public double confidence;
    public String transcript;
    //getters & setters
}
public class KeywordsResult{
    private List<Fine> fine;

}
public class Fine{
    private String normalized_text;
    private double start_time;
    private double end_time;
    private double confidence;
    //getters & setters
}

public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            // Convert JSON string to Object
            String jsonInString = "{\\r\\n  \\\"result_index\\\": 0,\\r\\n  \\\"results\\\": [\\r\\n{\\r\\n  \\\"final\\\": true,\\r\\n  \\\"alternatives\\\": [\\r\\n    {\\r\\n      \\\"confidence\\\": 0.715,\\r\\n      \\\"transcript\\\": \\\"hello how are you holds the medically I hope you are fine \\\"\\r\\n    }\\r\\n  ],\\r\\n  \\\"keywords_result\\\": {\\r\\n    \\\"fine\\\": [\\r\\n      {\\r\\n        \\\"normalized_text\\\": \\\"fine\\\",\\r\\n        \\\"start_time\\\": 5.85,\\r\\n        \\\"end_time\\\": 6.27,\\r\\n        \\\"confidence\\\": 0.918\\r\\n      }\\r\\n    ]\\r\\n  }\\r\\n}\\r\\n]\\r\\n}";
            Staff json2Object = mapper.readValue(jsonInString, Staff.class);
            // do whatever you want with object
        }catch (Exception e){
            // handle exception
        }
    }
  • Finally you have your object ready to get any prop you would like to, once implement your pojo and never have problems anymore!