如何从 Javascript 中的 JSON 对象获取键和值(邮递员)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53057372/
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-10-29 10:05:44  来源:igfitidea点击:

How to get Key and Value from JSON object in Javascript(Postman)

javascriptjsonpostmankey-value

提问by Raji Baskar

I have a JSON object like this, I wanna access the list array elements with key and value in postman.

我有一个这样的 JSON 对象,我想在邮递员中访问带有键和值的列表数组元素。

{
    "data": {
        "total": 1,
        "list": [
            {
                "id": 53,
                "name": "Sonu",
                "mobileNo": "6543213456",
                "address": "Greeny Pathway",
                "city": "NewYork",
                "mode": "Weekly",
                "duration": "15",
                "qty": null

            }
        ]
    },
    "success": true,
    "message": ""
}

How to separate it as Key and Value in Javascript like,

如何在 Javascript 中将其作为 Key 和 Value 分开,例如,

Key:   id,name,mobileNo,address,city,..
Value: 53,Sonu,6543213456,Greeny Pathway,NewYork,....

采纳答案by Raji Baskar

Finally this works for me!(In Postman Script)

最后这对我有用!(在邮递员脚本中)

var resdata = JSON.parse(responseBody);
console.log(resdata);

key = Object.keys(resdata.data.list[0]);
console.log(key);

value =Object.keys(resdata.data.list[0]);
console.log(value);

回答by Monarth Sarvaiya

First remove comma from line : "qty": null,otherwise it will cause error in json parsing.

首先从行中删除逗号: "qty": null,否则会导致json解析错误。

var resultJSON = `{
    "data": {
        "total": 1,
        "list": [
            {
                "id": 53,
                "name": "Sonu",
                "mobileNo": "6543213456",
                "address": "Greeny Pathway",
                "city": "NewYork",
                "mode": "Weekly",
                "duration": "15",
                "qty": null

            }
        ]
    },
    "success": true,
    "message": ""
}`;


var result = $.parseJSON(resultJSON);

var myList = result.data.list[0];

$.each(myList, function(k, v) {
    //display the key and value pair
    alert(k + ' is ' + v);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

回答by Raji Baskar

you can use below codes:

您可以使用以下代码:

const keys = Object.keys(jsonObject);
const values = Object.values(jsonObject);

But your JSON object is deep, you should flatten it and then use keysand valuesof Objectto make them separate.

但是,你的JSON对象是深,你应该将其压平,然后用keysvaluesObject,使它们分开。

回答by Prasanna Brabourame

You can get using key and value separately in a array.

您可以在数组中分别使用键和值。

var a = {
    "data": {
        "total": 1,
        "list": [
            {
                "id": 53,
                "name": "Sonu",
                "mobileNo": "6543213456",
                "address": "Greeny Pathway",
                "city": "NewYork",
                "mode": "Weekly",
                "duration": "15",
                "qty": null,

            }
        ]
    },
    "success": true,
    "message": ""
}

var keyval = Object.keys(a.data.list[0])
console.log(keyval)
var values = Object.values(a.data.list[0])
console.log(values)

回答by Ahsan Sohail

JSON objects are key value pair you cannot get the keys and values in object form as you desire but you can get both in form of arrays from this code

JSON 对象是键值对,您无法根据需要以对象形式获取键和值,但可以从此代码中以数组的形式获取两者

var key = []
var values = []
list.map(function(l){  keys = Object.getOwnPropertyNames(l); 
keys.map(function(key) {values.push(l[key]);})})