Javascript 使用 JSON 的键值对

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

Key value pairs using JSON

javascriptjqueryjson

提问by SLM

Is there a way to handle data structures using JSON object in a way of Key/ Value pairs?
If so can some one elaborate how to access associated value object from the key

有没有办法以键/值对的方式使用 JSON 对象处理数据结构?
如果是这样,有人可以详细说明如何从键访问关联的值对象

Assume that I have something like this

假设我有这样的事情

KEY1 |  VALUE OBJECT1 - (NAME: "XXXXXX", VALUE:100.0)  
KEY2 |  VALUE OBJECT2 - (NAME: "YYYYYYY", VALUE:200.0)  
KEY3 |  VALUE OBJECT3 - (NAME: "ZZZZZZZ", VALUE:500.0)  

回答by Guffa

A "JSON object" is actually an oxymoron. JSON is a text format describing an object, not an actual object, so data can either be in the form of JSON, or deserialised into an object.

“JSON 对象”实际上是一个矛盾的说法。JSON 是一种描述对象的文本格式,而不是实际的对象,因此数据可以采用 JSON 的形式,也可以反序列化为对象。

The JSON for that would look like this:

JSON 如下所示:

{"KEY1":{"NAME":"XXXXXX","VALUE":100},"KEY2":{"NAME":"YYYYYYY","VALUE":200},"KEY3":{"NAME":"ZZZZZZZ","VALUE":500}}

Once you have parsed the JSON into a Javascript object (called datain the code below), you can for example access the object for KEY2and it's properties like this:

一旦您将 JSON 解析为一个 Javascript 对象(data在下面的代码中调用),您可以例如访问该对象KEY2及其属性,如下所示:

var obj = data.KEY2;
alert(obj.NAME);
alert(obj.VALUE);

If you have the key as a string, you can use index notation:

如果您将键作为字符串,则可以使用索引表示法:

var key = 'KEY3';
var obj = data[key];

回答by RobertPitt

var object = {
    key1 : {
        name : 'xxxxxx',
        value : '100.0'
    },
    key2 : {
        name : 'yyyyyyy',
        value : '200.0'
    },
    key3 : {
        name : 'zzzzzz',
        value : '500.0'
    },
}

If thats how your object looks and you want to loop each name and value then I would try and do something like.

如果这就是您的对象的外观并且您想要循环每个名称和值,那么我会尝试做类似的事情。

$.each(object,function(key,innerjson){
    /*
        key would be key1,key2,key3
        innerjson would be the name and value **
    */

    //Alerts and logging of the variable.
    console.log(innerjson); //should show you the value    
    alert(innerjson.name); //Should say xxxxxx,yyyyyy,zzzzzzz
});

回答by jAndy

JSON(= JavaScript Object Notation), is a lightweight and fast mechanism to convert Javascript objects into a string and vice versa.

JSON(= ĴAVA小号CRIPT öbject Ñ浮选),是一种重量轻,快速机制转换JavaScript对象到字符串中,反之亦然。

Since Javascripts objects consists of key/valuepairs its very easy to use and access JSON that way.

由于 Javascripts 对象由key/value成对组成,因此它非常易于使用和访问 JSON。

So if we have an object:

所以如果我们有一个对象:

var myObj = {
    foo:   'bar',
    base:  'ball',
    deep:  {
       java:  'script'
    }
};

We can convert that into a string by calling window.JSON.stringify(myObj);with the result of "{"foo":"bar","base":"ball","deep":{"java":"script"}}".

我们可以转换成字符串通过调用window.JSON.stringify(myObj);与结果"{"foo":"bar","base":"ball","deep":{"java":"script"}}"

The other way around, we would call window.JSON.parse("a json string like the above");.

反过来,我们会调用window.JSON.parse("a json string like the above");.

JSON.parse()returns a javascript object/array on success.

JSON.parse()成功时返回一个 javascript 对象/数组。

alert(myObj.deep.java);  // 'script'

window.JSONis not natively available in all browser. Some "older" browser need a little javascript plugin which offers the above mentioned functionality. Check http://www.json.orgfor further information.

window.JSON并非在所有浏览器中都原生可用。一些“旧”浏览器需要一个提供上述功能的小 javascript 插件。查看http://www.json.org了解更多信息。

回答by Craig Gjerdingen

I see what you are trying to ask and I think this is the simplest answer to what you are looking for, given you might not know how many key pairs your are being sent.

我明白您要问什么,我认为这是您正在寻找的最简单的答案,因为您可能不知道要发送多少个密钥对。

Simple Key Pair JSON structure

简单的密钥对 JSON 结构

var data = {
    'XXXXXX' : '100.0',
    'YYYYYYY' : '200.0',
    'ZZZZZZZ' : '500.0',
}

Usage JavaScript code to access the key pairs

使用 JavaScript 代码访问密钥对

for (var key in data) 
  { if (!data.hasOwnProperty(key))
    { continue; } 
    console.log(key + ' -> ' +  data[key]);
  };

Console output should look like this

控制台输出应如下所示

XXXXXX -> 100.0 
YYYYYYY -> 200.0 
ZZZZZZZ -> 500.0

Here is a JSFiddleto show how it works.

这是一个JSFiddle来展示它是如何工作的。