简体   繁体   中英

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...

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:

[{"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"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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