使用JSON的键值对 [英] Key value pairs using JSON

查看:96
本文介绍了使用JSON的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种通过键/值对使用JSON对象处理数据结构的方法?
如果可以的话,可以详细说明如何从键

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

假设我有这样的东西

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

推荐答案

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

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如下所示:

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

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

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

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];

这篇关于使用JSON的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆