简体   繁体   English

正则表达式从响应中获取json数据

[英]Regex to get json data from the response

I have a json response like this 我有一个像这样的json响应

2019 May 28 10:03:56.586520 pacexg1v1 rdkbrowser2[4786]:  190528-10:03:56.586491 [mod=RDKBROWSER2, lvl=INFO] [tid=4786] onConsoleLog:rdkbrowser.cpp:926 [ConsoleAPI:144]: {"objectName":"com.comcast.BridgeObject_1","methodName":"JSMessageChanged","argv":["{\"action\":\"entitlementsAccountLink\",\"args\":{\"action\":\"appLaunch\",\"subscriptionEntitlements\":[{\"id\":\"Prime\",\"endDate\":1561629841000}]},\"pid\":7}"]}

From this using regex how can i get the json alone 从使用正则表达式的我如何才能单独获得json

{"objectName":"com.comcast.BridgeObject_1","methodName":"JSMessageChanged","argv":["{\"action\":\"entitlementsAccountLink\",\"args\":{\"action\":\"appLaunch\",\"subscriptionEntitlements\":[{\"id\":\"Prime\",\"endDate\":1561629841000}]},\"pid\":7}"]}

The following regular expression will do the work ({[\\s\\S]*}) when you have only one JSON data in a response and your response cannot contain { and } characters outside of the JSON data. 当您在响应中仅包含一个JSON数据并且响应在JSON数据之外不能包含{}字符时,以下正则表达式将起作用({[\\s\\S]*})

Otherwise you cannot resolve it with regular expressions, because a JSON data mostly contains recursion, ex. 否则,您将无法使用正则表达式进行解析,因为JSON数据主要包含递归,例如。 {"obj1": {"obj1_1": "val1_1"}} . {"obj1": {"obj1_1": "val1_1"}}

If you have two JSON data in a response you cannot tell where the first one ends and where the second one begins with regular expression. 如果响应中有两个JSON数据,则无法确定第一个结束于何处,第二个何处以正则表达式开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM