简体   繁体   English

如何在react js中将值对象转换为数组

[英]How to convert value object into array in react js

I have below kind of data.我有以下数据。

{"username":"[\\"abc\\"]","password":"[\\"asd\\"]"} . {"username":"[\\"abc\\"]","password":"[\\"asd\\"]"}

I want to convert it like我想把它转换成

{"username":"abc","password":"asd"} . {"username":"abc","password":"asd"}

I am trying JSON.stringify but its not working.我正在尝试 JSON.stringify 但它不起作用。

if you want to convert that obj to string you can use Regex replace如果您想将该 obj 转换为字符串,您可以使用 Regex 替换

JSON.stringify(z).replace(/(\\[\\\\"|\\\\"\\])/g,"");

 obj = { "username": "[\\"abc\\"]", "password": "[\\"asd\\"]" }; let result = JSON.stringify(obj).replace(/(\\[\\\\"|\\\\"\\])/g, ""); document.write(result);

or use this to remove characters & keep it Object not String或使用它来删除字符并保留它对象而不是字符串

 obj = { "username": "[\\"abc\\"]", "password": "[\\"asd\\"]" }; replace_=(s)=>s.replace(/(\\["|"\\])/g, ""); obj.password = replace_(obj.username); obj.username = replace_(obj.username); console.log(obj);

or或者

if you have array of objects & you want to remove those characters & keep the data as Objects use this如果您有对象数组&您想删除这些字符&保留数据作为对象使用这个

 ObjList=[ {"username":"[\\"ac\\"]","password":"[\\"ad\\"]"}, {"username":"[\\"abc\\"]","password":"[\\"asd\\"]"}, {"username":"[\\"abc\\"]","password":"[\\"asd\\"]"}, {"username":"[\\"abc\\"]","password":"[\\"asd\\"]"} ]; ObjList.forEach((v,i)=>{ ObjList[i].password=v.password.slice(2,-2); ObjList[i].username=v.username.slice(2,-2); }); console.log(ObjList);

Note: This has nothing to do with react and react cannot help you here.注意:这与 react 无关,react 在这里无法帮助您。

I assume your input data is JSON and you want to create JSON again.我假设您的输入数据是 JSON,并且您想再次创建 JSON。 The input data is not optimal, it looks like you double encoded the JSON somehow.输入数据不是最优的,看起来你以某种方式对 JSON 进行了双重编码。 Ideally it would look like this:理想情况下,它看起来像这样:

{"username":["abc"],"password":["asd"]}

Ie the values are actual arrays and not strings containing more JSON.即这些值是实际的数组,而不是包含更多 JSON 的字符串。

However, working with your original data, this is how it would go:但是,使用您的原始数据,它会是这样的:

  1. Parse the JSON into an object with JSON.parse .使用JSON.parse将 JSON 解析为一个对象。
  2. Parse the value of each property (which is again JSON) into arrays with JSON.parse .使用JSON.parse将每个属性的值(同样是 JSON)解析为数组。
  3. Assign the first element of the resulting arrays back to the properties.将结果数组的第一个元素分配回属性。
  4. Convert object to JSON with JSON.stringify .使用JSON.stringify将对象转换为 JSON。

Implementation:执行:

 // assuming `input` contains the JSON data, ie something like var input = String.raw`{"username":"[\\"abc\\"]","password":"[\\"asd\\"]"}`; var obj = JSON.parse(input); obj.username = JSON.parse(obj.username)[0]; obj.password = JSON.parse(obj.password)[0]; var output = JSON.stringify(obj); console.log(output);


If you manage to produce "better" input data (in which case why not generate it exactly as you need it though?) and don't have nested JSON but proper arrays as I have shown at the beginning, then the steps woul be the same except you wouldn't have to call JSON.parse on the properties.如果您设法生成“更好”的输入数据(在这种情况下,为什么不完全按照您的需要生成它?)并且没有嵌套的 JSON 而是我在开头所示的正确数组,那么步骤将是相同,但您不必在属性上调用JSON.parse

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

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