简体   繁体   English

如何将多个对象的字符串转换为具有不同键的数组

[英]How to Convert string of multiple object to in array with different keys

I have a string to multiple objects and I need to add this object in an array with different values.我有一个指向多个对象的字符串,我需要将此对象添加到具有不同值的数组中。 This data received from the cart so object can be 2,3,4...从购物车接收到的数据,因此对象可以是 2,3,4...

exmaple:例子:

{"hidden_product_name":"productA","productId":"120","product_sku":"","quantity":"2","hidden_unit_price":"500","hidden_total":"1300","total_pr_price":"1000"},{"hidden_product_name":"productB","productId":"127","product_sku":"","quantity":"1","hidden_unit_price":"300","hidden_total":"1300","total_pr_price":"300"}

this is a full string of two objects and I want the output like this.这是两个对象的完整字符串,我想要这样的输出。

Desire output:期望输出:

[{"hidden_product_name":"productA","productId":"120","product_sku":"","quantity":"2","hidden_unit_price":"500","hidden_total":"1300","total_pr_price":"1000"},{"hidden_product_name":"productB","productId":"127","product_sku":"","quantity":"1","hidden_unit_price":"300","hidden_total":"1300","total_pr_price":"300"}]

How can I do this please help...我该怎么做,请帮助...

I found the best and easiest way to resolve this problem...我找到了解决这个问题的最好和最简单的方法......

data = '{"hidden_product_name":"productA","productId":"120","product_sku":"","quantity":"2","hidden_unit_price":"500","hidden_total":"1300","total_pr_price":"1000"},{"hidden_product_name":"productB","productId":"127","product_sku":"","quantity":"1","hidden_unit_price":"300","hidden_total":"1300","total_pr_price":"300"}';

data2 = '['+data+']';

then data2 will return:然后 data2 将返回:

[{"hidden_product_name":"productA","productId":"120","product_sku":"","quantity":"2","hidden_unit_price":"500","hidden_total":"1300","total_pr_price":"1000"},{"hidden_product_name":"productB","productId":"127","product_sku":"","quantity":"1","hidden_unit_price":"300","hidden_total":"1300","total_pr_price":"300"}]

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

相关问题 使用字符串数组键转换对象 - convert object with string array keys 将具有不同键的 object 数组转换为单个 object - Convert an array of object with different keys to a single object 使用键和值将字符串数组转换为数组 Object - Convert String Array to Array Object with keys and values 如何将具有多个相同键的 srting 数组转换为 JSON Object? - How to convert array of srting with multiple same keys to JSON Object? 如何使用这些键及其值将具有一个 object 和多个键的数组转换为多个对象的数组? - How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values? 如何将带键的数组对象转换为数组(无键) - How to convert Array Object with keys to Arrays (No Keys) 是否可以使用 javascript 中的键将单个 object 数组转换为多个 object 数组? - Is it possible to convert an array of single object into an array of multiple object with keys in javascript? 如何将数组转换为 object 在第一个和最后一个数组元素上具有不同的键? - How do I convert an array to an object with different keys on the first and last array elements? 将 3 个键的 object 转换为数组 - convert object of 3 keys to array 如何使用键将数组转换为多个对象? - How to convert an array into multiple objects with keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM