提问者:小点点

为什么我不能验证这个包含包含命名对象的数组的JSON文档?


我试图创建一个JSON文档,其中包含一个对象数组,其中每个对象都有一个特定的“名称”,因此我创建了这个JSON文档:

{
    "forecast": [
        "morning": {
            "date": "01/10/2018",
            "min_temp": "12",
            "max_temp": "24",
            "humidity": "2",
            "wind_direction": "SO",
            "wind_force": "12",
            "status": "OK",
            "description": "sdfsdfsdf"
        },
        "afternoon": {
            "date": "01/10/2018",
            "min_temp": "",
            "max_temp": "",
            "humidity": "",
            "wind_direction": "",
            "wind_force": "",
            "status": "",
            "description": ""
        }
    ]
}

如您所见,forecast数组包含2个名为morning和午后的JSON对象。

但是通过这种方式,我获得了以下错误消息,试图在验证器(https://jsonlint.com/)上验证这个JSON文档,获得的错误是:

错误:第3行的分析错误:。。。演员阵容“:[“上午”:{“日期”:“01/1--------------------^应为'EOF'、'}'、'、']',获得':'

以这种格式更改以前的JSON文档(基本上删除JSON对象名称)它工作正常,并且正确验证:

{
    "forecast": [
        {
            "date": "01/10/2018",
            "min_temp": "12",
            "max_temp": "24",
            "humidity": "2",
            "wind_direction": "SO",
            "wind_force": "12",
            "status": "OK",
            "description": "sdfsdfsdf"
        },
        {
            "date": "01/10/2018",
            "min_temp": "",
            "max_temp": "",
            "humidity": "",
            "wind_direction": "",
            "wind_force": "",
            "status": "",
            "description": ""
        }
    ]
}

为什么第一个版本没有验证?我可以将对象名称放入JSON文档中的数组中吗?或者我必须添加上午、下午的信息作为每个对象的值字段?


共2个答案

匿名用户

您缺少数组元素周围的大括号:

{
    "forecast": [
      {  // HERE
        "morning": {
            "date": "01/10/2018",
            "min_temp": "12",
            "max_temp": "24",
            "humidity": "2",
            "wind_direction": "SO",
            "wind_force": "12",
            "status": "OK",
            "description": "sdfsdfsdf"
        },
        "afternoon": {
            "date": "01/10/2018",
            "min_temp": "",
            "max_temp": "",
            "humidity": "",
            "wind_direction": "",
            "wind_force": "",
            "status": "",
            "description": ""
        }
      }  // HERE
    ]
}

匿名用户

您拥有的不是有效的JSON。数组元素不能有“名称”,因为这只是一个对象。。。

{
    "forecast": {
        "morning": {
            "date": "01/10/2018",
            "min_temp": "12",
            "max_temp": "24",
            "humidity": "2",
            "wind_direction": "SO",
            "wind_force": "12",
            "status": "OK",
            "description": "sdfsdfsdf"
        },
        "afternoon": {
            "date": "01/10/2018",
            "min_temp": "",
            "max_temp": "",
            "humidity": "",
            "wind_direction": "",
            "wind_force": "",
            "status": "",
            "description": ""
        }
    }
}

另一种方法是将名称放入数组中的对象中。

{
    "forecast": [
        {
            "name": "morning",
            "date": "01/10/2018",
            "min_temp": "12",
            "max_temp": "24",
            "humidity": "2",
            "wind_direction": "SO",
            "wind_force": "12",
            "status": "OK",
            "description": "sdfsdfsdf"
        },
        {
            "name": "afternoon",
            "date": "01/10/2018",
            "min_temp": "",
            "max_temp": "",
            "humidity": "",
            "wind_direction": "",
            "wind_force": "",
            "status": "",
            "description": ""
        }
    }
}