首页 > 解决方案 > 将严重字符串化的 json 转换为 json 对象

问题描述

我有一些从网络服务中提取的数据。这是字符串

(正文:'3886' MessageProperties [headers={},timestamp=null,messageId=null,userId=null,receivedUserId=null,appId=null,clusterId=null,type=null,correlationId=null,correlationIdString=null,replyTo =null,contentType=application/x-java-serialized-object,contentEncoding=null,contentLength=0,deliveryMode=null,receivedDeliveryMode=PERSISTENT,expiration=null,priority=0,redelivered=false,receivedExchange=,receivedRoutingKey=bottomlesspit, receivedDelay=null,deliveryTag=62,messageCount=0,consumerTag=amq.ctag-sCwfLaMEqWp2GkFwFrY1yg,consumerQueue=bottomlesspit])

它看起来像 json,但键值对几乎Body没问题,但最重要的键与字符串所告诉的其他键不同。

我需要读取的值Body并能够获得这样的值

   console.log(d.body);
   //This above outputs the string as shown
   obj = eval('{' + d.body + '}');
   console.log(obj);
   var match = "Body";
   var val = obj.find( function(item) { return item.key == match } );
   console.log(val);

我如何读取密钥的值Body

标签: javascriptjson

解决方案


使用这个正则表达式而不是 match Body

\bBody:'(\d*)'

这将捕获第 1 组中的 Body 编号。


推荐阅读