javascript - 如何格式化这个json

标签 javascript json format

我正在为网站上的临时 TableView 编写一些 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"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
}

最佳答案

您的 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"
                }
            ]
        }
    ]
}

如果您想更改输出,请显示如何生成该输出以及您希望其外观的示例。

关于javascript - 如何格式化这个json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497754/

相关文章:

javascript - $injector 在使用 Angular 继承时不工作

json - 无法加载json映射Elasticsearch

mysql 求和格式

c# - NPOI 日期格式单元格

javascript - 如何映射此 Json 以与 Handlebars js 一起使用?

java - 正确格式化我的输出

javascript - 文本堆叠在自身之上

javascript - 处理 Rally 数据存储中的空异常

javascript - Chrome 扩展程序意外隐藏 YouTube 中的按钮

ruby-on-rails - Swift 将 json 发布到 Rails