0

从下面的 JSON 文件中,我想提取X TimeY Time. 最简单的方法是什么?

{
    "Timestamp": "Mon Mar 16 21:37:22 EDT 2015",
    "Event": "Reporting  Time",
    "Message": "load for http://xxx.xx.xx.xx:1xxxx/operations&proxy=www.mywebsite.co.nz&send=https://xxx.xx.xx.xx:xxxx took (X Time: 306 ms, Y Time: 1923 ms)StatusCode: Unknown<br>Cookies: nzh_weatherlocation=12; dax_ppv=11|NZH:home|NZH:home|NZH:home|9|undefined; Safari/537.36<br>CPUs: 2<br>Language: en-GB",

}
4

3 回答 3

1

你可以使用 grep。

$ grep -o '[YX]\s*Time:\s*[^,)]*' file
X Time: 306 ms
Y Time: 1923 ms
  • [YX]将匹配XY

  • \s*匹配零个或多个空格字符。

  • Time:\s*匹配字符串Time加上后面的零个或多个空格。

  • [^,)]*匹配任何字符但不匹配逗号或)0 次或多次的否定字符类。

于 2015-03-18T03:44:56.480 回答
0

这是一个使用capture的 jq 解决方案:

  .Message
| capture("X Time: (?<X Time>[^,]+), Y Time: (?<Y Time>[^)]+)")

如果此过滤器在filter.jq且输入数据在input.json

jq -M -f filter.jq input.json

会产生

{
  "X Time": "306 ms",
  "Y Time": "1923 ms"
}
于 2017-08-24T20:17:23.483 回答
-1

First of all, i recomend to your Gson or Jackson Libraries (are used to serialize/deserialize) json objects easier.

But, if i were you i make it manually of this way:

JsonObject MyJson= new JsonObject(yourstring);

1.- To obtain the X Time or Y Time:

String FullMessage=MyJson.getString("message");

int XPosition=FullMessage.indexOf("X Time:");
int YPosition=FullMessage.indexOf("Y Time:");
int EndYPosition=FullMessage.indexOf("ms)");

String XTime=FullMessage.substring(XPosition,Yposition);
String YTime=FullMessage.subString(Yposition,EndYPosition);

/* of that way you could obtain x and y data, i havent tested it, but the main idea i write. */

2.-And to Write the url is easy, just make it:

MyObject.putString("url","www.mywebsite.co.nz"); //and thats all

But i recommend to use and Serializer/deserializer library, its easier and better.

Regards.

于 2015-03-18T03:53:13.783 回答