开发者

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

开发者 https://www.devze.com 2023-01-08 11:00 出处:网络
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):

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"
    }
 },
开发者_Python百科 "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.


Since you are able to send the json data to php, you can do convert it using php too with its json_decode function.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号