如何通过在此JSON结果中进行比较来获取值

时间:2013-10-26 16:34:45

标签: javascript jquery arrays json

我有this JSON。我需要首先将teams中的值与html_content.position[i]中的值进行比较,其中i应该是循环变量,然后如果比较返回true,则获取值。见这个例子:

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

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

"img\/magallanes.jpg"

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

2 个答案:

答案 0 :(得分: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 :(以前的参考仅在第三方网站上发布)

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"},
         ...
      ]
      ...
}

答案 1 :(得分: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);
});