0

我的JSON有什么问题

{
    "id":1,
    "album":"127 Hours",
    "songs":
        [{
            "id":1,
            "name":"Never Hear Surf Music Again",
            "duration":"5:52"
        }],
    "id":2,
    "album":"Adele 21",
    "songs":
        [{
            "id":1,
            "name":"Rolling In The Deep",
            "duration":"03:48"
        }]
}

在我的应用程序中使用JSON之前,我在这里检查它:

http://json.parser.online.fr/

但只为 id: 2 获取JSON Eval,而不是为id: 1 & 2

4

5 回答 5

4

You can't have a property more than once in an object, and you have twice the "id" property, twice "album" and twice "songs".

Even if it's a valid (parsable) JSON string, it doesn't store what you want it to store, or more precisely it can't be restored in an object with all those property values.

You should make it an array of objects :

[
    {

        "id":1,
        "album":"127 Hours",
        "songs":[
          {
          "id":1,
          "name":"Never Hear Surf Music Again",
          "duration":"5:52"
          }
        ]
    }, {
        "id":2,
        "album":"Adele 21",
        "songs":[
          {
          "id":1,
          "name":"Rolling In The Deep",
          "duration":"03:48"
          }
        ]
    }
 ]
于 2013-06-25T10:34:55.833 回答
2

您有一个顶级对象,每个键 和 有id两个album映射songs。这是无效的 - 对象的键应该是唯一的。

您可能需要顶层是对象列表

[
    {
        "id":1,
        "album":"127 Hours",
        "songs": [
            {
                "id":1,
                "name":"Never Hear Surf Music Again",
                "duration":"5:52"
            }
        ]
    },
    {
        "id":2,
        .....

或稍微更改表示并使用由专辑 ID 键入的顶级对象

{
   "album_1":{
        "title":"127 Hours",
        "songs": [
            {
                "id":1,
                "name":"Never Hear Surf Music Again",
                "duration":"5:52"
            }
        ]
    },
    "album_2":{
        "title":"Adele 21",
         ....

哪种表示更合适取决于您的用例。对象列表表示允许您以可预测的顺序遍历专辑列表,但这意味着您需要进行(线性或二进制)搜索以找到具有特定 ID 的专辑。顶级对象表示使您可以更轻松地找到单个专辑 ID 的详细信息,但不再保证在迭代时以相同的顺序获取它们。

于 2013-06-25T10:40:00.003 回答
2
{
    "Album list": [
        {
                "id":1,
             "album":"127 Hours",
             "songs":{
    "id":1,
    "name":"Never Hear Surf Music Again",
    "duration":"5:52"
    }
        },
        {
               "id":2,
    "album":"Adele 21",
    "songs":

    {
    "id":1,
    "name":"Rolling In The Deep",
    "duration":"03:48"
    }
        }



    ]
}
于 2013-06-25T10:47:11.527 回答
1

I believe this is how your JSON should be

[{
    "id": 1,
    "album": "127 Hours",
    "songs": [{
        "id": 1,
        "name": "Never Hear Surf Music Again",
        "duration": "5:52"
    }]
}, {
    "id": 2,
    "album": "Adele 21",
    "songs": [{
        "id": 1,
        "name": "Rolling In The Deep",
        "duration": "03:48"
    }]
}]
于 2013-06-25T10:36:40.317 回答
1

我为您的 JSON 使用适当的缩进编辑了您的问题。您现在可以很容易地看到您有重复的属性,特别idalbumsongs。在有效的 JSON 中,您不能有重复的属性名称 - 因此您的错误。

最有可能的是,您希望拥有一组对象而不是所有东西。我怀疑,以下 JSON 将满足您的需求:

[{
    "id":1,
    "album":"127 Hours",
    "songs":
        [{
            "id":1,
            "name":"Never Hear Surf Music Again",
            "duration":"5:52"
        }]
},
{
    "id":2,
    "album":"Adele 21",
    "songs":
        [{
            "id":1,
            "name":"Rolling In The Deep",
            "duration":"03:48"
        }]
}]
于 2013-06-25T10:38:42.310 回答