将JSON对象的表示形式更改为显式键/值格式(Change representation of JSON object to explicit key/value format)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
将JSON对象的表示形式更改为显式键/值格式(Change representation of JSON object to explicit key/value format)

我有以下JSON:

{ "temperature": "22.2", "power": "6", "current": "156" }

我需要将它转换为这个显式结构:

{ "key": "temperature", "value": "22.2" }, { "key": "power", "value": "6" }, { "key": "current", "value": "156" }

这是一种优雅,简单,快捷的方法吗?

最好的,thx

I have the following JSON:

{ "temperature": "22.2", "power": "6", "current": "156" }

and I need to convert it to this explicit structure:

{ "key": "temperature", "value": "22.2" }, { "key": "power", "value": "6" }, { "key": "current", "value": "156" }

Is there an elegant, simple and quick way to do this?

Best, thx

最满意答案

var newStructure = Object.keys(obj).map(function(key){ return {'key':key, 'value':obj[key]} })

var obj = {
    "temperature": "22.2",
    "power": "6",
    "current": "156"
}

var arr = Object.keys(obj).map(function(key){return {'key':key,'value':obj[key]}})

console.log(arr) 
  
 

var newStructure = Object.keys(obj).map(function(key){ return {'key':key, 'value':obj[key]} })

Example

var obj = {
    "temperature": "22.2",
    "power": "6",
    "current": "156"
}

var arr = Object.keys(obj).map(function(key){return {'key':key,'value':obj[key]}})

console.log(arr) 
  
 

更多推荐

本文发布于:2023-04-20 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2e41ae8a45d515ae64ee6ddb1d7f52a6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   形式   格式   JSON   显式键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!