检查json数据的值,然后获取其密钥

时间:2021-03-27 23:45:01

I have some JSON data that I am retrieving from https://status.mojang.com/check and am storing in a variable. I'm still quite new to JSON/JS and I can't seem to find any answers on google.

我有一些JSON数据,我从https://status.mojang.com/check检索并存储在变量中。我还是JSON / JS的新手,我似乎无法在谷歌上找到任何答案。

Code:

码:

function checkMojang() {
 var mojangStatus = mojang.status();
 mojangStatus.then(function (message) {
     var response = JSON.parse(message);
 })
}

Data I am using can be seen at the link above. I am trying to check all the data in the json array, see if any of the values contain "yellow" or "red" and get the keys for those values along with their checked value but can't figure out how to do so.

我正在使用的数据可以在上面的链接中看到。我试图检查json数组中的所有数据,看看是否有任何值包含“黄色”或“红色”并获取这些值的键及其检查值,但无法弄清楚如何执行此操作。

3 个解决方案

#1


1  

You can loop through the array and then through the object properties and make a new object using the colors as keys

您可以遍历数组,然后遍历对象属性,并使用颜色作为键创建一个新对象

var response = [{"minecraft.net":"green"},{"session.minecraft.net":"red"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"yellow"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"red"}];

var new_response = {};
response.forEach(function(obj){
  for (var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
	  if(new_response[obj[prop]] == undefined) new_response[obj[prop]] = [];
      new_response[obj[prop]].push(prop);
    }
  }
})
console.log(new_response);

The you can use the object for your needs as

您可以根据需要使用该对象

new_response["red"]

giving you the list of all key with red value.

为您提供具有红色值的所有键的列表。

#2


1  

you can use the method array.foreach() to execute a provided function once per array element and the for ... in to itarate over the enumarable properties. So you can test the value and get keys for the value "yellow" or "red"

你可以使用方法array.foreach()为每个数组元素执行一次提供的函数,并使用for ... in itarate over the enumarable properties。因此,您可以测试值并获取值“黄色”或“红色”的键

response.forEach(function(element) {
    for (k in element) {
        if (element[k]=="red" or element[k]=="yellow") {
            // k is the key
        }
    }
});

#3


0  

function checkMojang() {
    var mojangStatus = mojang.status();
    mojangStatus.then(function (message) {
        var response = JSON.parse(message);

        for (i = 0; i < response.length; i++) { // iterate over response array
            var item = response[i];             // get item from array
            var key = Object.keys(item)[0];     // get the key of the item
            var value = item[key];              // get the value of the item

            if (value === 'yellow' || value === 'red') {
                // do something, like adding it to a list
            }
        }
    });
}

#1


1  

You can loop through the array and then through the object properties and make a new object using the colors as keys

您可以遍历数组,然后遍历对象属性,并使用颜色作为键创建一个新对象

var response = [{"minecraft.net":"green"},{"session.minecraft.net":"red"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"yellow"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"red"}];

var new_response = {};
response.forEach(function(obj){
  for (var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
	  if(new_response[obj[prop]] == undefined) new_response[obj[prop]] = [];
      new_response[obj[prop]].push(prop);
    }
  }
})
console.log(new_response);

The you can use the object for your needs as

您可以根据需要使用该对象

new_response["red"]

giving you the list of all key with red value.

为您提供具有红色值的所有键的列表。

#2


1  

you can use the method array.foreach() to execute a provided function once per array element and the for ... in to itarate over the enumarable properties. So you can test the value and get keys for the value "yellow" or "red"

你可以使用方法array.foreach()为每个数组元素执行一次提供的函数,并使用for ... in itarate over the enumarable properties。因此,您可以测试值并获取值“黄色”或“红色”的键

response.forEach(function(element) {
    for (k in element) {
        if (element[k]=="red" or element[k]=="yellow") {
            // k is the key
        }
    }
});

#3


0  

function checkMojang() {
    var mojangStatus = mojang.status();
    mojangStatus.then(function (message) {
        var response = JSON.parse(message);

        for (i = 0; i < response.length; i++) { // iterate over response array
            var item = response[i];             // get item from array
            var key = Object.keys(item)[0];     // get the key of the item
            var value = item[key];              // get the value of the item

            if (value === 'yellow' || value === 'red') {
                // do something, like adding it to a list
            }
        }
    });
}