如何从 NodeJS JSON 中的特定键获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41949461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:58:04  来源:igfitidea点击:

How to get value from specific key in NodeJS JSON

jsonnode.js

提问by PPShein

{
    "id": 83,
    "key": "hello",
    "en": "Hello",
    "mm": "This is greeting",
    "created_at": "2016-12-05T10:14:02.928Z",
    "updated_at": "2017-01-31T02:57:11.181Z"
}

I'm trying to get value from mmkey in NodeJS. I've tried to get translation["mm"]but it does not work at all. Please help me how to get mmfrom above data.

我正在尝试从mmNodeJS 中的键中获取值。我试图得到,translation["mm"]但它根本不起作用。请帮助我如何mm从上述数据中获取。

My current node version is 5.11

我当前的节点版本是 5.11

回答by Vikas

Edited The Answer. Now Its working for above object in question

编辑了答案。现在它适用于上述对象

You can use following function to access the keys of JSON. I have returned 'mm' key specifically.

您可以使用以下函数来访问 JSON 的键。我专门返回了“mm”键。

    function jsonParser(stringValue) {

       var string = JSON.stringify(stringValue);
       var objectValue = JSON.parse(string);
       return objectValue['mm'];
    }

回答by adonike

JSON is an interchange format, meaning it's only a text representing data in a format that many applications can read and translate into it's own language.

JSON 是一种交换格式,这意味着它只是一种以许多应用程序可以读取并翻译成它自己的语言的格式表示数据的文本。

Therefore, using node.js you should parse it first (translate it to a javascript object) and store the result in a variable:

因此,使用 node.js 您应该首先解析它(将其转换为 javascript 对象)并将结果存储在一个变量中:

var obj = JSON.parse(yourJSONString);

Then, you can ask for any of it properties:

然后,您可以询问它的任何属性:

var mm = obj.mm;