简体   繁体   English

在Node.js中拆分对象

[英]Split object in Node.js

Yesterday i solve my problem spliting my string with "\\" but today i have the same problem but with a object... 昨天我解决了用“ \\”分割字符串的问题,但是今天我遇到了同样的问题,但是有一个对象...

2|wscontro | [2017-05-31 15:57:23.145] - debug: /opt/wscontroller/wscontroller-api/routes/ubus UbusController 63320169-611e-43f5-880e-9b1a13152cfd getDeviceServicesById signature {"config":"wireless","section":"radio0","values":"{\"disabled\":0}"}
2|wscontro | [2017-05-31 15:57:23.145] - debug: /opt/wscontroller/wscontroller-api/routes/ubus UbusController 63320169-611e-43f5-880e-9b1a13152cfd getDeviceServicesById signature "object"

I need to have only signature => {"config":"wireless","section":"radio0","values":{"disabled":0"}} 我只需要签名=> {“ config”:“ wireless”,“ section”:“ radio0”,“ values”:{“ disabled”:0“}}

Can anyone help me? 谁能帮我? I try to convert to String this object and split doing 我尝试将此对象转换为String并拆分

var aux = signature.split('\\').join('');
var jsonObject = JSON.parse(aux);

But i get the same result {"config":"wireless","section":"radio0","values":"{\\"disabled\\":0"}} 但是我得到相同的结果{“ config”:“ wireless”,“ section”:“ radio0”,“ values”:“ {\\” disabled \\“:0”}}

My last post: Split string by "\\" Node.js 我的最后一篇文章: 通过“ \\” Node.js分割字符串

anyone can help? 有人可以帮忙吗?

is this you wanted? 这是你想要的吗?

 var str =' {"config":"wireless","section":"radio0","values":"{\\"disabled\\":0"}'; console.log(str.replace(/\\\\/g,'')); 

Your object should be like you said in your last comment {"config":"wireless","section":"radio0","values":"{\\"disable‌​d\\":0}"} then: 您的对象应该像您在上一条注释{“ config”:“ wireless”,“ section”:“ radio0”,“ values”:“ {\\” disable‌ d \\“:0}”}中所说的一样,然后:

var jsonstring = "{"config":"wireless","section":"radio0","values":"{\"disable‌​d\":0}"}";
var escapedJsonstring = jsonstring.split('\\').join('');
var json = JSON.parse(escapedJsonstring);
json.parsedValues = JSON.parse(json.values);
console.log(json);

Finally you have parsed object in json variable. 最后,您在json变量中解析了对象。 The main idea is that the values attribute has also string value and not object value. 主要思想是values属性还具有字符串值而不是对象值。 So you need to parse it again as JSON. 因此,您需要再次将其解析为JSON。 Result of this parsing is stored in json.parsedValues, but you can rewrite the values string with object using this: json.values = JSON.parse(json.values); 解析的结果存储在json.parsedValues中,但是您可以使用以下方法用对象重写values字符串:json.values = JSON.parse(json.values);

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

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