开发者

JSON List of Tags/Values

开发者 https://www.devze.com 2023-01-17 09:52 出处:网络
I am working on a project that requires an object to have multiple values. For instance, a list of \'things\' all with a name but each thing also has another category, \"Tags\" with could include thin

I am working on a project that requires an object to have multiple values. For instance, a list of 'things' all with a name but each thing also has another category, "Tags" with could include things like round, circle, ball, sport, etc. all words describing what the 'thing' is. I am trying to define this in JSON but I keep running into errors with jsonlint. Take a look:

{
"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"}]
}]
}

Hopefully you see what I am trying to accomplish.

Thanks for your help!

Anthony


Your "tags" objects seem off to me. The code:

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

would try to create an array of an object whose properties don't have any values, but I'm not sure that's even valid syntax. Do you want an array of words for "tags?" If so, you need to drop those curly braces:

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

That would give you an object with property "things" that contains an array of one object with a "name" and a "tags" property, where "tags" is an array of words.

You could access "tags" like so:

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


If each thing has a name then why not use that name as a tag as follows:

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

Now you can refer to things.thing1 or things.thing2 and the tags as things.thing1[0] and things.thing2[2] etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消