简体   繁体   中英

Converting a multi-dimensional array (javascript) to JSON (or similar) for transport

I'm generating a multi-dimensional array in javascript that looks like this (this is the JSON representation of the javascript array, it's not in JSON format):

"100": {
 "40": {
    "subtotal": "24.99",
    "turn-around": {
        "0": "2-4 Business Days",
        "1": "Next Business Day (Add $15.00)"
    },
    "shipping": {
        "0": "UPS Ground - $0.00",
        "1": "UPS 2nd Day Air - $14.73",
        "2": "UPS 3 Day Select - $13.13"
    }
 },
 "41": {
    "subtotal": "29.99",
    "turn-around": {
        "0": "2-4 Business Days",
        "1": "Next Business Day (Add $15.00)"
    },
    "shipping": {
        "0": "UPS Ground - $0.00",
        "1": "UPS 2nd Day Air - $14.73",
        "2": "UPS 3 Day Select - $13.13"
    }
 }
}

I'm trying to convert this to JSON format, so I can import this to a PHP script. I'm using the JSON.stringify, but only getting the result:

[null,null,null,null,null,null,null,null,... CLIP... null,null,null,null,null,null,null,null,null,[]]]

I'm pretty sure the array's correct, because when dumping the contents, I get this:

'1000' ...
'41' ...
    'subtotal' => "$24.00"
    'tat' ...
        '0' => "- Choose Turnaround Time -"
        '1' => "Next Business Day (Add $15.00)"
        '2' => "2-4 Business Days"
    'shipping' ...
        '0' => "FREE UPS Ground - $0.00"
        '1' => "UPS 2nd Day Air - $12.75"
        '2' => "UPS 3 Day Select - $13.13"
        '3' => "UPS Next Day Air Saver - $15.32"
        '4' => "UPS Next Day Air - $17.04"
        '5' => "UPS Next Day Air Early A.M. - $71.61"

I'm not sure why the JSON.stringify method is not working. All I need it to get the array into a readable format to digest in PHP. Perhaps there's a better way?

All I need is to get a multi-dimensional array in javascript to a multi-dimensional array in PHP. I'm not a javascript expert, so that could be the real problem here.

由于您能够将json数据发送到php,因此您也可以使用php的json_decode函数将其转换。

JSON stands for JavaSript Object Notation. which means that it can be used to carry any kind of data that can be represented using JavaScript's data structures (hashes and arrays) which means that these are all valid JSON objects:

foo = {
    "spam" : "eggs",
    "Yello" : [ "w", "Dello" ]
}
bar = [
    "Green",
    "Eggs",
    "Ham",
    { "Ron" : "Burgundy" }
]

So, if your array is already valid JSON there's no need to call stringify on it. And from what I can see, you're already good to go!

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