Try with JSON library
sample code:
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject innJsonObject = jsonObject.getJSONArray("entries").getJSONObject(0);
System.out.println(innJsonObject.get("pageref")); // page_0
System.out.println(innJsonObject.get("time")); // 515
You can try with GSON library as well.
sample code:
Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<Map<String, Object>>>>() {}.getType();
Map<String, ArrayList<Map<String, Object>>> data = gson.fromJson(reader, type);
Map<String, Object> map = data.get("entries").get(0);
System.out.println(map.get("pageref")); // page_0
System.out.println(map.get("time")); // 515.0
This is the valid JSON string:
{
"entries": [
{
"pageref": "page_0",
"startedDateTime": "2014-07-21T21: 08: 37.491+05: 30",
"time": 515,
"request": {
"method": "GET",
"url": "https: //www.facebook.com/",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [
{
"name": "Host",
"value": "www.facebook.com"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0(WindowsNT6.1;rv: 30.0)Gecko/20100101Firefox/30.0"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.5"
},
{
"name": "Accept-Encoding",
"value": "gzip,deflate"
},
{
"name": "Connection",
"value": "keep-alive"
}
]
}
}
]
}
8
solved RegEx in Java for multiple hits [closed]