简体   繁体   English

如何将JSON数据存储到JavaScript中的键值对中?

[英]how to store JSON data into key-value pairs in javascript?

how to store JSON data into key-value pairs in javascript? 如何将JSON数据存储到JavaScript中的键值对中?

I have a json data file : data.js It contains follwing data 我有一个json数据文件:data.js它包含以下数据

[
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]

how to store this data in key-value pair data-structure in java script? 如何在Java脚本的键值对数据结构中存储此数据?

You can use Array.map or Array.reduce (see MDN ) to map the Array elements to an object, something like: 您可以使用Array.mapArray.reduce (请参阅MDN )将Array元素映射到对象,例如:

 var objmap = {}, objred = {}; // parse the string (from your file) var objFromFile = JSON.parse('[\\ { "Sound": "0.550951615" },\\ { "Battery": 0.2567320563 },\\ { "Screen": 0.0703125 },\\ { "RAM": 0.1246117102 },\\ { "Connectivity": 0 },\\ { "Camera": 0.2721623766 },\\ { "Design": 0.3677622768 },\\ { "Processor": 0 }\\ ]'); // and map it to [obj] var mapped = objFromFile.map( function (v) { for (var l in v) {this[l] = v[l];} }, objmap ); // or use reduce if you prefer it var reduced = objFromFile.reduce( function(p, n) { for (var l in n) {p[l] = n[l];} return p; }, objred ); // show the result, using JSON.stringify document.querySelector('#result').textContent = 'mapped:\\n' + JSON.stringify(objmap, null, ' ') + '\\n\\nreduced:\\n' + JSON.stringify(objred, null, ' '); 
 <pre id="result"></pre> 

JSON stands for JavaScript Object Notation. JSON代表JavaScript对象符号。 This is a systematic Syntax to store data. 这是存储数据的系统语法。

for storing single value: 用于存储单个值:

var Object1 = {
  "Key": "Value"
}

for storing multiple value: 用于存储多个值:

var Object1 = {
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value3",
  "nth key": "nth value"
}

You need to process your data structure to get this done. 您需要处理数据结构才能完成此任务。

var a = [
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]
var keyValue = {}
a.forEach(function(e) {
    keyValue[Object.keys(e)[0]] = Object.values(e)[0]
})

Output of console.log(keyValue) console.log(keyValue)输出

{"Sound":"0.550951615","Battery":0.2567320563,"Screen":0.0703125,"RAM":0.1246117102,"Connectivity":0,"Camera":0.2721623766,"Design":0.3677622768,"Processor":0}
var myObject = { 
 key: value,
 key: value
 };

You would literally just list your contents in that format. 您实际上只是以这种格式列出您的内容。

Other Resources: http://www.w3schools.com/js/js_objects.asp 其他资源: http : //www.w3schools.com/js/js_objects.asp

How to "properly" create a custom object in JavaScript? 如何在JavaScript中“正确”创建自定义对象?

You're already storing the data as key:value. 您已经将数据存储为key:value。

If you're wondering how you can access the various bits and pieces of your data, checkout this article: JavaScript loop through json array? 如果您想知道如何访问数据的各个部分,请查看本文: JavaScript通过JSON数组循环吗?

OR ... 要么 ...

You can find a particular k:v pair by index. 您可以通过索引找到特定的k:v对。 In your case, if you store the payload in a variable, you could access the data like so: 在您的情况下,如果将有效负载存储在变量中,则可以像这样访问数据:

var data = [
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]

data[0]

This would return { "Sound": "0.550951615" } 这将返回{“ Sound”:“ 0.550951615”}

There are a TON of resources that you can search for JSON references. 您可以搜索大量资源来搜索JSON引用。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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