首页 > 解决方案 > 使用 Node.js 从 JSON 中搜索和获取价值

问题描述

我需要从下面的 json 中搜索“信息”并使用 node.js 获得如下响应。

请帮助解决这个问题。

JSON文件

{
      "call": "Info",
      "locator": "test.so",
      "classname": "DeviceInfo",
      "autostart": true,
      "state": "activated",
       "module": "Plugin",
}

回复

Info,test.so,DeviceInfo,true,activated,Plugin

我在下面尝试过并且只打印信息而不是所有数据。我需要使用信息搜索而不是调用

var fs = require("fs");
var content = fs.readFileSync("D:/Test1.json");
var objectValue = JSON.parse(content);
console.log(objectValue.call);

标签: node.js

解决方案


您可以Object.values(...)按照此处所述使用:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

var fs = require("fs");
var content = fs.readFileSync("D:/Test1.json");
var objectValue = JSON.parse(content);
// output: [ 'Info', 'test.so', 'DeviceInfo', true, 'activated', 'Plugin' ]


推荐阅读