JSON标签/值列表

时间:2010-09-26 04:30:59

标签: json data-structures jsonp

我正在开发一个需要对象具有多个值的项目。例如,所有带有名称的“东西”列表,但每个东西也有另一个类别,“标签”可以包括圆形,圆形,球形,运动等等所有单词,描述“东西”是什么。我试图用JSON定义它,但我一直遇到jsonlint错误。看看:

{
"things":[{
    "name":"thing1",
    "tags":[{"globe","circle","round","world"}]
},
{
    "name":"thing2",
    "tags":[{"cloud","weather","lightning","sky"}]
},
{
    "name":"thing3",
    "tags":[{"round","bullseye","target"}]
},
{
    "name":"thing4",
    "tags":[{"round","key","lock"}]
},
{
    "name":"thing5",
    "tags":[{"weather","sun","sky","summer"}]
}]
}

希望你能看到我想要完成的任务。

感谢您的帮助!

安东尼

2 个答案:

答案 0 :(得分:3)

你的“标签”对象似乎不适合我。代码:

[{"globe","circle","round","world"}]

会尝试创建一个对象的数组,该对象的属性没有任何值,但我不确定它是否是有效的语法。你想要一个“标签”的单词数组吗?如果是这样,你需要删除那些花括号:

var myThings = {
    "things":[{
        "name":"thing1",
        "tags":["globe","circle","round","world"]
    }]
};

这会给你一个带有属性“things”的对象,它包含一个带有“name”和“tags”属性的对象的数组,其中“tags”是一个单词数组。

您可以像这样访问“标签”:

var thing1tag2 = myThings.things[0].tags[1]; // = "circle"

答案 1 :(得分:1)

如果每个东西都有一个名称,那么为什么不将该名称用作标记,如下所示:

var things = {
  "thing1": ["globe", "circle", "round", "world"],
  "thing2": ["cloud", "weather", "lightning", "sky"],
  "thing3": ["round", "bullseye", "target"],
  "thing4": ["round", "key", "lock"],
  "thing5", ["weather", "sun", "sky", "summer"]
};

现在,您可以引用things.thing1things.thing2,将代码称为things.thing1[0]things.thing2[2]等。