如何格式化这个json

时间:2013-10-21 14:50:47

标签: javascript json format

我正在为网站上的临时表视图编写一些JSON。从来没有写过JSON,所以非常感谢一些帮助。

我需要显示每年分成18组的船员名单,然后将这些名单分成2组,每组9个,有4个属性。

层次结构看起来像这样:

  • 2013
    • 结果
    • 蓝船
      • 职位,姓名,大学,体重
      • (+ 8以上)
    • Isis(预定船)
  • 2012
  • 2011
  • (......自1829年起)

这是我第一次尝试编写/格式化JSON,所以请仅在您有帮助或有建设性时发表评论。

JSON

{
"crews": [{
    "items": [
    {
        "year"      :   "2013",
        "boat"      :   "Blue",
        "position"  :   "1",
        "name"      :   "Patrick Close",
        "college"   :   "Pembroke",
        "weight"    :   "14st 2lbs"
    }, {
        "year"      :   "2013",
        "boat"      :   "Blue",
        "position"  :   "2",
        "name"      :   "Geordie Macleod",
        "college"   :   "Christ Church",
        "weight"    :   "13st 10lbs"
    }, {
        "year"      :   "2013",
        "boat"      :   "Isis",
        "position"  :   "1",
        "name"      :   "Iain Mandale",
        "college"   :   "Mansfield",
        "weight"    :   "11st 11lbs"
    }, {
        "year"      :   "2013",
        "boat"      :   "Isis",
        "position"  :   "2",
        "name"      :   "Nichola Hazell",
        "college"   :   "Christ Church",
        "weight"    :   "14st 9lbs"
    }]
}
}

这样更好吗?

{
"crews": [
    {
        "year": [
            {
                "2013": [
                    {
                        "boat": [
                            {
                                "Blue": [
                                    {
                                        "boat"      :   "Blue",
                                        "position"  :   "1",
                                        "name"      :   "Patrick Close",
                                        "college"   :   "Pembroke",
                                        "weight"    :   "14st 2lbs"
                                    },
                                    {
                                        "boat"      :   "Blue",
                                        "position"  :   "2",
                                        "name"      :   "Geordie Macleod",
                                        "college"   :   "Christ Church",
                                        "weight"    :   "13st 10lbs"
                                    }
                                ],
                                "Isis": [
                                    {
                                        "boat"      :   "Isis",
                                        "position"  :   "1",
                                        "name"      :   "Iain Mandale",
                                        "college"   :   "Mansfield",
                                        "weight"    :   "11st 11lbs"
                                    },
                                    {
                                        "year"      :   "2013",
                                        "boat"      :   "Isis",
                                        "position"  :   "2",
                                        "name"      :   "Nichola Hazell",
                                        "college"   :   "Christ Church",
                                        "weight"    :   "14st 9lbs"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
}

1 个答案:

答案 0 :(得分:0)

您的JSON无效。您可以在JSONLint.com进行测试,修正:

{
    "crews": [
        {
            "items": [
                {
                    "year": "2013",
                    "boat": "Blue",
                    "position": "1",
                    "name": "Patrick Close",
                    "college": "Pembroke",
                    "weight": "14st 2lbs"
                },
                {
                    "year": "2013",
                    "boat": "Blue",
                    "position": "2",
                    "name": "Geordie Macleod",
                    "college": "Christ Church",
                    "weight": "13st 10lbs"
                },
                {
                    "year": "2013",
                    "boat": "Isis",
                    "position": "1",
                    "name": "Iain Mandale",
                    "college": "Mansfield",
                    "weight": "11st 11lbs"
                },
                {
                    "year": "2013",
                    "boat": "Isis",
                    "position": "2",
                    "name": "Nichola Hazell",
                    "college": "Christ Church",
                    "weight": "14st 9lbs"
                }
            ]
        }
    ]
}

如果你想改变输出显示你如何生成这个以及你希望它看起来像什么的例子。