像关系SQL数据库(Javascript)一样使用JSon [英] Using JSon like a Relational SQL Database (Javascript)

查看:79
本文介绍了像关系SQL数据库(Javascript)一样使用JSon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个看起来像这样的JSON变量:

So I have a JSON variable that looks like this:

var peopleList = {
    "1": {"Name": "Lisa", "item1": "Name of Item 1"}   ,
    "2": {"Name": "Marty"}   ,
    "3": {"Name": "Jordan",  "item1":"Name of Item 1",  "item2":"Name of Item 2"}
}

我想这有点像关系数据库。

I guess it's kind of like a relational database.

基本上我正在尝试做的程序范围我需要能够继续添加多个人(此变量中的1-3个),然后将多个项目关联到这些人。

Basically for the scope of the program i'm trying to do I need to be able to keep adding in multiple people (1-3 in this variable) and then associate multiple items to those people.

此外,对于变量,因为它是现在我甚至不确定Javascript中的getter和setter是什么。

Also for the variable as it is now i'm not even sure what the getters and setters would be in Javascript.

例如,我如何将项目添加到Marty

For example, how would I add an item to "Marty"

我如何从乔丹人打印出item2的名称

And how would I print out the Name for item2 from person "Jordan"

感谢您的帮助。我对JSON还是有点新鲜。

Thanks for any help. I'm still a little bit new to JSON.

如果有更好的方法可以解决这个问题,那么我就是耳朵。

And if there is a better way to do this that is easier to parse, i'm all ears.

推荐答案

获取的任何对象的值。

 peopleList[3].Name // will return Jordan

将任何键值对添加到任何对象尝试此

to add any key value pair to any object try this

var MartyData = peopleList[2];
MartyData['item1'] = "Name of item 1 of Marty";
// you can check now that data added or not
alert(peopleList[2].item1); // return Name of item1 of marty

DEMO

这篇关于像关系SQL数据库(Javascript)一样使用JSon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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