简体   繁体   English

将多维数组(javascript)转换为JSON(或类似格式)以进行传输

[英]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): 我正在用javascript生成一个如下所示的多维数组(这是javascript数组的JSON表示形式,它不是JSON格式):

"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. 我正在尝试将其转换为JSON格式,因此可以将其导入到PHP脚本中。 I'm using the JSON.stringify, but only getting the result: 我正在使用JSON.stringify,但只得到结果:

[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. 我不确定JSON.stringify方法为何不起作用。 All I need it to get the array into a readable format to digest in PHP. 我所需要的只是将数组转换为可读格式以在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. 我所需要的只是将JavaScript中的多维数组转换为PHP中的多维数组。 I'm not a javascript expert, so that could be the real problem here. 我不是JavaScript专家,所以这可能是真正的问题。

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

JSON stands for JavaSript Object Notation. JSON代表JavaSript对象表示法。 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: 这意味着它可以用于承载可以使用JavaScript的数据结构(哈希和数组)表示的任何类型的数据,这意味着这些都是有效的JSON对象:

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. 因此,如果您的数组已经是有效的JSON,则无需在其上调用stringify。 And from what I can see, you're already good to go! 从我所看到的来看,您已经很高兴了!

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

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