Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何构造JSON?_Json - Fatal编程技术网

如何构造JSON?

如何构造JSON?,json,Json,我有这个数据样本,我需要把它转换成JSON格式 这样做的最佳方式/结构是什么?如果有帮助,我将为此开发一个角度产品选择工具 Item 1: Federation Phaser Options: | FORM FACTOR | PRICE | | Compact | $545 | | Pistol Grip | $600 | Item 2: Sith Lightsaber Options: | BLADE COLOR | BLAD

我有这个数据样本,我需要把它转换成JSON格式

这样做的最佳方式/结构是什么?如果有帮助,我将为此开发一个角度产品选择工具

Item 1: Federation Phaser
Options:
  | FORM FACTOR   | PRICE |
  | Compact             | $545  |
  | Pistol Grip          | $600  |

Item 2: Sith Lightsaber
Options:
  | BLADE COLOR | BLADE COUNT | PRICE |
  | Red                    | Single                 | $1000 |
  | Red                    | Double               | $1750 |
  | Blue                   | Single                 | $1125 |
  | Blue                   | Double                | $1875 |
  | Green                | Single                  | $1250 |

JSON由名称/值对组成,并由大括号{}包围。名称/值对由逗号分隔,值本身可以是JSON对象或数组

示例1(简单):

示例2(更复杂):

对于您的示例,您可以使用数组按如下方式构造JSON:

{
    "item": {
        "name": "Federation Phaser",
        "options": [
            {
                "form": "compact",
                "price": "$545"
            },
            {
                "form": "Pistol Grip",
                "price": "$600"
            }
        ]
    },
    "item2": {
        "name": "Sith Lightsaber",
        "options": [
            {
                "bladeColor": "red",
                "count": "single",
                "price": "$1000"
            },
            {
                "bladeColor": "blue",
                "count": "double",
                "price": "$1875"
            }
        ]
    }
}
如果您想拥有数量可变的“项”,也可以将它们放入数组中。例如:

{
    "items": [
        {
            "name": "Federation Phaser",
            "options": [{
                    "form": "compact",
                    "price": "$545"
                },
                {
                    "form": "Pistol Grip",
                    "price": "$600"
                }
            ]
        },
        {
            "name": "Sith Lightsaber",
            "options": [{
                    "bladeColor": "red",
                    "count": "single",
                    "price": "$1000"
                },
                {
                    "bladeColor": "blue",
                    "count": "double",
                    "price": "$1875"
                }
            ]
        }
    ]
}

这正是我需要的。我只是不知道如何将选项添加到这样的数组中。非常感谢。可能重复的
{
    "item": {
        "name": "Federation Phaser",
        "options": [
            {
                "form": "compact",
                "price": "$545"
            },
            {
                "form": "Pistol Grip",
                "price": "$600"
            }
        ]
    },
    "item2": {
        "name": "Sith Lightsaber",
        "options": [
            {
                "bladeColor": "red",
                "count": "single",
                "price": "$1000"
            },
            {
                "bladeColor": "blue",
                "count": "double",
                "price": "$1875"
            }
        ]
    }
}
{
    "items": [
        {
            "name": "Federation Phaser",
            "options": [{
                    "form": "compact",
                    "price": "$545"
                },
                {
                    "form": "Pistol Grip",
                    "price": "$600"
                }
            ]
        },
        {
            "name": "Sith Lightsaber",
            "options": [{
                    "bladeColor": "red",
                    "count": "single",
                    "price": "$1000"
                },
                {
                    "bladeColor": "blue",
                    "count": "double",
                    "price": "$1875"
                }
            ]
        }
    ]
}