简体   繁体   中英

How can i search for an specified result[key + value] in a json with javascript?

I am developing a discord bot, which works with the shoppy api, right now. My target is to filter the respone of the shoppy api and get a value and key. All i have done is parsing the request result into an json object. This is my code - i replaces sensitive data

request("https://shoppy.gg/api/v1/orders/"+args[0], options ,function(error, response, body) {
      if (*1) {
        console.log("Result: " + body);
        var info = JSON.parse(body);

        const embed = new RichEmbed()
        .setColor("#756cff")
        .setDescription("Valid order ID. You have been given customer role!")
        .setImage(client.user.displayAvatarURL);

      message.channel.send(embed);

      }else{
        const embed = new RichEmbed()
        .setColor("#756cff")
        .setDescription("Please enter a valid ID!")
        .setImage(client.user.displayAvatarURL);

      message.channel.send(embed);;
      }
    });

So i want that *1 checks if the result is {"status":false,"message":"Requested resource not found"}. How can i do this?

我不明白你的问题,但如果你需要对象的“键”和“值”,你可以使用 Object.entries(objectName) 如果你需要找到对象的“值”并且你知道对象的名称,你可以使用: objectName.key 或 objectName[“key”]

Just use a simple if statement to check if the property and value are in the object or not

 var obj={as:534,dsad:54,htyhty:65,delivered:1} obj.delivered==1?console.log(true):console.log(false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM