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

发布于 2024-09-11 02:06:58 字数 1726 浏览 2 评论 0原文

我正在 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"
    }
 }
}

我正在尝试将其转换为 JSON 格式,因此我可以将其导入PHP 脚本。我正在使用 JSON.stringify,但只得到结果:

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

我很确定数组是正确的,因为当 转储内容,我得到这个:

'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"

我不确定为什么 JSON.stringify 方法不起作用。我所需要的只是将数组转换为可读的格式,以便在 PHP 中进行消化。也许有更好的方法?

我所需要的只是将 javascript 中的多维数组转换为 PHP 中的多维数组。我不是 javascript 专家,所以这可能是真正的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-09-18 02:06:58

由于您可以将 json 数据发送到 php,因此您也可以使用 php 对其 json_decode 函数。

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

柠北森屋 2024-09-18 02:06:58

JSON 代表 JavaSript 对象表示法。这意味着它可以用于携带可以使用 JavaScript 的数据结构(哈希和数组)表示的任何类型的数据,这意味着这些都是有效的 JSON 对象:

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

因此,如果您的数组已经是 有效的 JSON 无需对其调用 stringify。据我所知,您已经可以开始了!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文