0

我有这个 JSON。我需要首先将 inteams的值与html_content.position[i]wherei应该是循环 var 的值进行比较,然后如果比较返回 true 则获取该值。看这个例子:

html_content.position[0][0].toLowerCase() = "navegantes";

然后我应该与每个值进行比较,teams如果有任何 es 相等的键,那么我应该得到这个值,举个例子,我应该得到这个值:

"img\/magallanes.jpg"

有人可以在这里给我一些帮助吗?

4

2 回答 2

1

问题在于您正在使用的这段代码:

html_content.position[0][0].toLowerCase() = "navegantes";

html_content.position实际上是一个数组数组的数组(尽管在您的示例数据中,每个内部数组的长度仅为 1 ......这总是真的吗?),所以您需要一个括号运算符来测试html_content.position[i][0][0]

这是一个包含您的 JSON 数据的示例,查看每个团队,并找到相应的position:(请参阅下面的工作 JSfiddle 演示)

解决方案代码:

var matches = [];

for(var teamName in json.teams)
{
   for(var i = 0, len = json.html_content.position.length; i < len; i++)
   {
      if(json.html_content.position[i][0][0].toLowerCase() === teamName.toLowerCase())
      {
          // found a match. do something with it..
          matches[matches.length] = {Name: teamName, Value: json.teams[teamName]};
          break;
      }
   }
}

JSON:(供参考,因为以前仅在第 3 方网站上发布)

var json = {
      "response":true,
      "teams":{
         "navegantes":"img\/magallanes.jpg",
         "tigres":"img\/tigres.jpg",
         "caribes":"img\/caribes.jpg",
         "leones":"img\/leones.jpg",
         "aguilas":"img\/aguilas.jpg",
         "tiburones":"img\/tiburones.jpg",
         "bravos":"img\/bravos.jpg",
         "cardenales":"img\/cardenales.jpg",
         "maga":"img\/magallanes.jpg",
         "tigr":"img\/tigres.jpg",
         "cari":"img\/caribes.jpg",
         "leon":"img\/leones.jpg",
         "agui":"img\/aguilas.jpg",
         "tibu":"img\/tiburones.jpg",
         "brav":"img\/bravos.jpg",
         "card":"img\/cardenales.jpg"
      },
      "html_content":{
         "position":[
            [
               [
                  "Navegantes",
                  "14",
                  "10",
                  "4",
                  "0"
               ]
            ],
            [
               [
                  "Tigres",
                  "14",
                  "10",
                  "4",
                  "0"
               ]
            ],
            [
               [
                  "Caribes",
                  "14",
                  "9",
                  "5",
                  "1"
               ]
            ],
            [
               [
                  "Leones",
                  "14",
                  "9",
                  "5",
                  "1"
               ]
            ],
            [
               [
                  "Tiburones",
                  "13",
                  "5",
                  "8",
                  "4.5"
               ]
            ],
            [
               [
                  "Aguilas",
                  "14",
                  "5",
                  "9",
                  "5"
               ]
            ],
            [
               [
                  "Bravos",
                  "14",
                  "4",
                  "10",
                  "6"
               ]
            ],
            [
               [
                  "Cardenales",
                  "13",
                  "3",
                  "10",
                  "6.5"
               ]
            ]
         ],
         "current":[
            [
               "MAGA",
               "CARI",
               "7:00 pm",
               "PUERTO LA CRUZ"
            ],
            [
               "AGUI",
               "LEON",
               "4:00 pm",
               "CARACAS"
            ],
            [
               "BRAV",
               "TIGR",
               "5:30 pm",
               "MARACAY"
            ],
            [
               "TIBU",
               "CARD",
               "5:30 pm",
               "BARQUISIMETO"
            ]
         ],
         "next":[
            [
               "MAGA",
               "CARI",
               "6:00 pm",
               "PUERTO LA CRUZ"
            ],
            [
               "AGUI",
               "LEON",
               "1:00 pm",
               "CARACAS"
            ],
            [
               "TIBU",
               "TIGR",
               "5:30 pm",
               "MARACAY"
            ],
            [
               "BRAV",
               "CARD",
               "2:00 pm",
               "BARQUISIMETO"
            ]
         ],
         "previous":[

         ]
      }
   };

在此处查看示例小提琴:http: //jsfiddle.net/VqHpJ/

示例小提琴创建了一个数组,matches该数组是代表团队的对象集合NameValue相关图像的集合,并将它们吐出到列表中。(当然,您并没有说找到匹配项后要做什么,因此您可以根据需要进行调整)。

这是匹配两个数组(一个嵌套循环)中的元素的相当典型的模式,在我的示例中,它假设应该只找到一个匹配项,在这种情况下,它将break跳出嵌套循环并开始寻找下一个团队。因此,在最坏的情况下,性能为 O(n^2)。

这有点棘手(但并不困难),因为该teams对象并没有真正的团队数组,它具有每个团队名称的特定属性。因此,解决方案是teams使用for(var teamName in json.teams). 如果您是生成 JSON 的函数的作者,您可能需要考虑修改它以生成一组团队,例如:

var json = {
      "response":true,
      "teams":
      [
         { "Name": "navegantes", "ImageUrl": "img\/magallanes.jpg" },
         { "Name": "tigres", "ImageUrl": "img\/tigres.jpg"},
         { "Name": "caribes", "ImageUrl": "img\/caribes.jpg"},
         ...
      ]
      ...
}
于 2013-10-26T16:57:00.410 回答
0

假设您有一个 JSON 字符串,您将其解析为如下所示的对象

var json = {"teams":{
         "navegantes":"img\/magallanes.jpg",
         "tigres":"img\/tigres.jpg",
         "caribes":"img\/caribes.jpg",
         "leones":"img\/leones.jpg",
         "aguilas":"img\/aguilas.jpg",
         ...
}};

您可以迭代使用each()

$.each(json.teams, function(i, v){
   console.log(i);
   console.log(v);
});
于 2013-10-26T16:45:53.747 回答