用于多次点击的Java中的RegEx

时间:2014-07-22 18:17:59

标签: java regex

输入字符串包含多次出现的" pageref"和"时间" 给我一个java中的正则表达式或java中的源代码,从输入字符串中提取它们的值。下面给出了输入字符串的一部分。

输入字符串:

 "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 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/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"
        }
      ],

需要输出:

page_0 515

1 个答案:

答案 0 :(得分:0)

尝试使用JSON library

示例代码:

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

您也可以尝试使用GSON库。

示例代码:

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

这是有效的JSON字符串:

{
    "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"
                    }
                ]
            }
        }
    ]
}