简体   繁体   中英

How can I create a json string from an array of json strings without double encoding?

In PHP, I have an array of JSON strings. I would like to know if there is a way to create a JSON string of this array without double encoding the original JSON.

The array looks like this:
在此处输入图片说明

The resulting call to json_encode($myArray, JSON_UNESCAPED_SLASHES) looks like this:

{
    "invoice": "{\"order_id\":\"1\",\"store_id\":\"1\",\"customer_id\":\"2\",\"billing_address_id\":\"2\",\"shipping_address_id\":\"1\",\"global_currency_code\":\"USD\",\"base_currency_code\":\"USD\",\"store_currency_code\":\"USD\",\"order_currency_code\":\"USD\",\"store_to_base_rate\":\"0.0000\",\"store_to_order_rate\":\"0.0000\",\"base_to_global_rate\":\"1.0000\",\"base_to_order_rate\":\"1.0000\",\"items\":{},\"total_qty\":1,\"subtotal\":59.95,\"base_subtotal\":59.95,\"subtotal_incl_tax\":59.95,\"base_subtotal_incl_tax\":59.95,\"grand_total\":64.95,\"base_grand_total\":64.95,\"discount_amount\":0,\"base_discount_amount\":0,\"shipping_amount\":\"5.0000\",\"base_shipping_amount\":\"5.0000\",\"shipping_incl_tax\":\"5.0000\",\"base_shipping_incl_tax\":\"5.0000\",\"shipping_tax_amount\":\"0.0000\",\"base_shipping_tax_amount\":\"0.0000\",\"shipping_discount_tax_compensation_amount\":\"0.0000\",\"base_shipping_discount_tax_compensation_amnt\":null,\"tax_amount\":0,\"base_tax_amount\":0,\"discount_tax_compensation_amount\":0,\"base_discount_tax_compensation_amount\":0,\"base_cost\":0,\"can_void_flag\":false,\"state\":2}",
    "items": "{\"totalRecords\":0,\"items\":[{\"order_item_id\":\"1\",\"product_id\":\"1\",\"sku\":\"TP1\",\"name\":\"Test Product ONE\",\"description\":null,\"price\":\"59.9500\",\"base_price\":\"59.9500\",\"base_cost\":null,\"price_incl_tax\":\"59.9500\",\"base_price_incl_tax\":\"59.9500\",\"weee_tax_applied\":\"[]\",\"weee_tax_applied_amount\":null,\"weee_tax_applied_row_amount\":0,\"base_weee_tax_applied_amount\":null,\"base_weee_tax_applied_row_amnt\":null,\"weee_tax_disposition\":null,\"base_weee_tax_disposition\":null,\"weee_tax_row_disposition\":0,\"base_weee_tax_row_disposition\":0,\"qty\":\"1.000000\",\"invoice\":{},\"parent_id\":null,\"store_id\":\"1\",\"row_total\":59.95,\"base_row_total\":59.95,\"row_total_incl_tax\":59.95,\"base_row_total_incl_tax\":59.95,\"tax_amount\":0,\"base_tax_amount\":0,\"discount_tax_compensation_amount\":0,\"base_discount_tax_compensation_amount\":0,\"base_weee_tax_applied_row_amount\":0}]}",
    "billing-address": "{\"entity_id\":\"2\",\"parent_id\":\"1\",\"customer_address_id\":\"3\",\"quote_address_id\":null,\"region_id\":\"24\",\"customer_id\":null,\"fax\":null,\"region\":\"Indiana\",\"postcode\":\"47933\",\"lastname\":\"Thompson\",\"street\":\"204 N Grace Avenue\",\"city\":\"Crawfordsville\",\"email\":\"mitch@stdfasdfti.org\",\"telephone\":\"(987)987-9877\",\"country_id\":\"US\",\"firstname\":\"Mitchell\",\"address_type\":\"billing\",\"prefix\":null,\"middlename\":null,\"suffix\":null,\"company\":null,\"vat_id\":null,\"vat_is_valid\":null,\"vat_request_id\":null,\"vat_request_date\":null,\"vat_request_success\":null}",
    "shipping-address": "{\"entity_id\":\"1\",\"parent_id\":\"1\",\"customer_address_id\":\"3\",\"quote_address_id\":null,\"region_id\":\"24\",\"customer_id\":null,\"fax\":null,\"region\":\"Indiana\",\"postcode\":\"47933\",\"lastname\":\"Thompson\",\"street\":\"204 N Grace Avenue\",\"city\":\"Crawfordsville\",\"email\":\"mitch@sasdfdsfatti.org\",\"telephone\":\"(987)987-9877\",\"country_id\":\"US\",\"firstname\":\"Mitchell\",\"address_type\":\"shipping\",\"prefix\":null,\"middlename\":null,\"suffix\":null,\"company\":null,\"vat_id\":null,\"vat_is_valid\":null,\"vat_request_id\":null,\"vat_request_date\":null,\"vat_request_success\":null}"
}

I understand WHY its doing it this way, i'd just rather it not. Also, ideally i'd like to avoid using a str_replace. Ideas?

First decode your data, then encode all

$subitem = array_map('json_decode', $subitem);
echo json_encode($subitem);

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