【问题标题】:Parsing JSON Multiple Levels With JavaScript使用 JavaScript 解析 JSON 多层次
【发布时间】:2014-01-28 17:53:42
【问题描述】:

您好,目前我正在尝试获取特定值,但不完全确定如何获取。

以下是我正在使用的 JSON 数据的一部分。数据设置为等于“条目”

 var entries = "credits": {
    "crew": [
        {
            "id": 7469,
            "name": "JimUhls",
            "department": "Writing",
            "job": "Author",
            "profile_path": null
        },
        {
            "id": 7474,
            "name": "RossGraysonBell",
            "department": "Production",
            "job": "Producer",
            "profile_path": null
        },
        {
            "id": 7475,
            "name": "CeánChaffin",
            "department": "Production",
            "job": "Producer",
            "profile_path": null
        },
        {
            "id": 1254,
            "name": "ArtLinson",
            "department": "Production",
            "job": "Producer",
            "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
        },
        {
            "id": 7477,
            "name": "JohnKing",
            "department": "Sound",
            "job": "OriginalMusicComposer",
            "profile_path": null
        },
        {
            "id": 7478,
            "name": "MichaelSimpson",
            "department": "Sound",
            "job": "OriginalMusicComposer",
            "profile_path": null
        },
        {
            "id": 7479,
            "name": "JeffCronenweth",
            "department": "Camera",
            "job": "DirectorofPhotography",
            "profile_path": null
        },
        {
            "id": 7480,
            "name": "JamesHaygood",
            "department": "Editing",
            "job": "Editor",
            "profile_path": null
        },
        {
            "id": 7481,
            "name": "LarayMayfield",
            "department": "Production",
            "job": "Casting",
            "profile_path": null
        },
        {
            "id": 1303,
            "name": "AlexMcDowell",
            "department": "Art",
            "job": "ProductionDesign",
            "profile_path": null
        },
        {
            "id": 7763,
            "name": "RenKlyce",
            "department": "Sound",
            "job": "SoundEditor",
            "profile_path": null
        },
        {
            "id": 7764,
            "name": "RichardHymns",
            "department": "Sound",
            "job": "SoundEditor",
            "profile_path": null
        },
        {
            "id": 7467,
            "name": "DavidFincher",
            "department": "Directing",
            "job": "Director",
            "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
        },
        {
            "id": 7468,
            "name": "ChuckPalahniuk",
            "department": "Writing",
            "job": "Novel",
            "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
        }
    ]
}
}

然后我使用这个来解析数据:

crew_member = 0,
crew_members = [];

for (crew_member = 0; crew_member < entries.credits.crew.length; crew_member++) {
    crew_members.push(entries.credits.crew[crew_member].job + ': ' + entries.credits.crew[crew_member].name);
}

document.getElementById('Crew').innerHTML = crew_members.join(',');

一切都对我有用。我遇到的问题是如何具体获取例如导演和导演。

【问题讨论】:

  • 那真的是你的 JSON 吗?开头缺少{,所以语法无效。
  • @Barmar 不只是其中的一部分
  • 你说的只有director是什么意思,没有这个字段。
  • @VinayAggarwal 是的,看看 id 7467 Darpartment:directing & Job:director
  • 那么为什么不做一个 if 语句来查看 crew[i].job == "director" 是否?

标签: javascript jquery json parsing themoviedb-api


【解决方案1】:

这是一个简单的if 声明:

for (crew_member = 0; crew_member < entries.credits.crew.length; crew_member++) {
    if (entries.credits.crew[crew_member].job == "Director") {
        crew_members.push(entries.credits.crew[crew_member].job + ': ' + entries.credits.crew[crew_member].name);
    }
}

【讨论】:

  • 谢谢你,工作完美!
【解决方案2】:

试试jquery

jQuery.grep(entries.credits.crew,function(n,i){ return (n.job == "Director") });

这应该会返回你作为导演的工作人员元素数组。

谢谢。

【讨论】:

    【解决方案3】:

    最简单的方法

    entries.credits.crew.filter(function(a){ return a.job == 'Director'; });
    

    您将获得一个单独的对象数组,其中 job 是“Director”

    【讨论】:

      【解决方案4】:

      您可以过滤项目并仍然像这样返回和数组:

      entries.credits.crew.filter(function(member){ return member.job === "DirectorofPhotography"  } );
      

      你也可以添加这个:

       entries.credits.crew.filter(function(member){ return member.job === "DirectorofPhotography"  } ).map(function(member){
       return member.job+ ': ' + member.name;}).join(',');
      

      更新 1 这是基于此对象,因为提供的似乎格式不正确:

      var entries = { "credits" : {"crew":[
                  {
                      "id": 7469,
                      "name": "JimUhls",
                      "department": "Writing",
                      "job": "Author",
                      "profile_path": null
                  },
                  {
                      "id": 7474,
                      "name": "RossGraysonBell",
                      "department": "Production",
                      "job": "Producer",
                      "profile_path": null
                  },
                  {
                      "id": 7475,
                      "name": "CeánChaffin",
                      "department": "Production",
                      "job": "Producer",
                      "profile_path": null
                  },
                  {
                      "id": 1254,
                      "name": "ArtLinson",
                      "department": "Production",
                      "job": "Producer",
                      "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
                  },
                  {
                      "id": 7477,
                      "name": "JohnKing",
                      "department": "Sound",
                      "job": "OriginalMusicComposer",
                      "profile_path": null
                  },
                  {
                      "id": 7478,
                      "name": "MichaelSimpson",
                      "department": "Sound",
                      "job": "OriginalMusicComposer",
                      "profile_path": null
                  },
                  {
                      "id": 7479,
                      "name": "JeffCronenweth",
                      "department": "Camera",
                      "job": "DirectorofPhotography",
                      "profile_path": null
                  },
                  {
                      "id": 7480,
                      "name": "JamesHaygood",
                      "department": "Editing",
                      "job": "Editor",
                      "profile_path": null
                  },
                  {
                      "id": 7481,
                      "name": "LarayMayfield",
                      "department": "Production",
                      "job": "Casting",
                      "profile_path": null
                  },
                  {
                      "id": 1303,
                      "name": "AlexMcDowell",
                      "department": "Art",
                      "job": "ProductionDesign",
                      "profile_path": null
                  },
                  {
                      "id": 7763,
                      "name": "RenKlyce",
                      "department": "Sound",
                      "job": "SoundEditor",
                      "profile_path": null
                  },
                  {
                      "id": 7764,
                      "name": "RichardHymns",
                      "department": "Sound",
                      "job": "SoundEditor",
                      "profile_path": null
                  },
                  {
                      "id": 7467,
                      "name": "DavidFincher",
                      "department": "Directing",
                      "job": "Director",
                      "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
                  },
                  {
                      "id": 7468,
                      "name": "ChuckPalahniuk",
                      "department": "Writing",
                      "job": "Novel",
                      "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
                  }
              ]
          } };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-18
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多