-3

I am currently using bufferreader to read an API documentation. Below is part of the output:

"number": 88, 
"results": [
    {
        "time": "2013-04-15T18:05:02", 
        "name": "..."
    }, 
    {
        "time": "2013-05-01T18:05:00", 
        "name": "..."
    }, 
    ...
]     

If I want to extract only "2013-04-15T18:05:02", which is the date. How can I do that?

user21478621
  • 71
  • 1
  • 9

2 Answers2

0

You can use minimal json.

The following snippet extracts the dates and id's of all items:

JsonObject object = Json.parse(output).asObject();
JsonArray results = object.get("results").asArray();
for (JsonValue item : results) {
  String date = item.asObject().getString("date", "");
  String id = item.asObject().getString("id", "");
  ...  
}
hvardhan
  • 460
  • 8
  • 14
  • I am currently working on this problem on coderpad. I wonder w/o importing the json files into directory and such, what do I actually import on coderpad in order to get the job done? – user21478621 Feb 04 '17 at 03:24
  • You can parse JSON in Java w/o importing anything. [http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – hvardhan Feb 04 '17 at 03:28
  • Sorry I am new to this. This is still quite confusing. When I was trying to create JsonObject, coderpad said "cannot find symbol" which confused me. – user21478621 Feb 04 '17 at 03:46
  • For using JSON in Java natively, follow this link: [http://stackoverflow.com/questions/2591098/how-to-parse-json‌​-in-java](http://stackoverflow.com/questions/2591098/how-to-parse-json‌​-in-java) . For using `minimal json` you will have to follow it's setup instructions. – hvardhan Feb 04 '17 at 04:32
0

The format of your string seems to be JSON. You can use Jackson API to parse the JSON string into an array. If you don't want to use Jackson or other JSON API, you can still do it using some of the java.util.String class methods. Checkout the following sample:

List<String> dates = new ArrayList<String>();
String results = jsonString.substring(jsonString.indexOf("results"));
while((int index = results.indexOf("\\"date\\"")) != -1) {

   String date = results.substring(results.indexOf(':', index), results.indexOf(',', index)).replaceAll(" ", "").replaceAll("\\"", "");
   dates.add(date);
   results = results.substring(results.indexOf(',', index));

}
VHS
  • 9,534
  • 3
  • 19
  • 43