2010-09-26 46 views
0

我正在開發一個需要對象具有多個值的項目。例如,「事物」名單中都有一個名字,但每個事物都有另一個類別,「標籤」可以包括諸如圓形,圓形,球形,運動等等的所有詞語,用於描述「事物」是什麼。我試圖在JSON中定義這個,但是我一直用jsonlint運行錯誤。請看:JSON標籤/值列表

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

}

希望你明白我試圖完成。

感謝您的幫助!

安東尼

回答

3

您的「標籤」對象似乎對我來說。代碼:

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

會嘗試創建一個對象,其屬性沒有任何值的數組,但我不知道這是連有效的語法。你想爲「標籤」添加一組單詞嗎?如果是這樣,您需要刪除那些花括號:

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

這將使你使用包含一個對象的數組具有「name」屬性「東西」的對象和「標籤」性質,其中「標籤「是一組單詞。

你可以訪問「標籤」,例如:

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

是的,從標籤中刪除大括號。 – 2010-09-26 05:15:01

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]等。