2012-03-25 48 views
0

嗨,我有它返回的web服務 Web服務的結構就是這樣一個返回JSON數據:如何從返回JSON從Web服務訪問數據的JavaScript或jQuery的

jsonp1332655154667({"products": [{"uid": "37", 
"samsid": "hjk", 
"name": "Science%20Essentials%2010%20AC%20edn", 
"shortname": "scienceessentials10", 
"description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20", 
"generated": "3/25/2012%205:59:19%20AM", 
"Description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20", 
"PublishingCompany": "Macmillan%20Australia", 
"Service": "OneStopScience", 
"Service": "OneStopDigital", 
"Icon": "http://curriculumplatform.s3.amazonaws.com/prod/icons/brain48.png", 
"Country": "Australia", 
"Shortname": "scienceessentials10", 
"MarketingSite": "http%3a%2f%2fwww.macmillan.com.au%2fsecondary%2fonix%2fall%2f6F597241EFC0E43DCA257791001CAFC0%3fopen%26div%3dSecondary%26cat%3dScience%253EAustralian%252BCurriculum%26template%3ddomSecondary%26ed%3dsite%2fseced31.nsf", 
"Skin": "OneStopScience%20Green"}, 
"tag":"s_science"' 
"tag":"s_maths"' 
"tag":"s_arts", 
{"uid": "5",}]}) 

我有三個「標籤」元素。但是當我們訪問products.tag時,它總是給出最後一個元素,如:s_arts。 有什麼方法可以找出所有的元素,例如:s_science,s_maths,s_arts。 請幫忙。

+0

嘗試'console.log(products.tag)'並告訴我們會發生什麼 – Joseph 2012-03-25 06:12:15

+0

是不是無效的JSON? – bernie 2012-03-25 06:14:09

+0

它給出標籤:s_arts標籤的最後一個元素。 – 2012-03-25 06:14:57

回答

0

如果你在同一個對象中有多個鍵,你將會得到未定義的行爲。只有一個會被保留下來,因爲對沒有排序,所以你不能保證你會得到哪一個。

簡而言之:web服務正在返回錯誤數據。如果多個標籤預計,該服務應在標籤屬性返回值的數組:

... 
"tag":["s_science", "s_maths", "s_arts"], 
... 
1

它是無效的JSON,你的標籤應該是:

..., 
"tag": ["s_science", "s_maths", "s_arts" ], 
... 

然後product.tag將是一個數組,你可以訪問成功

問候

+0

我不確定它本身是無效的,但它只能拿一個,我發現它總是需要定義最後一個。 – Tuan 2012-03-25 06:31:01

0

您需要發送標籤作爲數組:

jsonp1332655154667({"products": [{"uid": "37", 
"samsid": "hjk", 
"name": "Science%20Essentials%2010%20AC%20edn", 
"shortname": "scienceessentials10", 
"description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20", 
"generated": "3/25/2012%205:59:19%20AM", 
"Description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20", 
"PublishingCompany": "Macmillan%20Australia", 
"Service": "OneStopScience", 
"Service": "OneStopDigital", 
"Icon": "http://curriculumplatform.s3.amazonaws.com/prod/icons/brain48.png", 
"Country": "Australia", 
"Shortname": "scienceessentials10", 
"MarketingSite": "http%3a%2f%2fwww.macmillan.com.au%2fsecondary%2fonix%2fall%2f6F597241EFC0E43DCA257791001CAFC0%3fopen%26div%3dSecondary%26cat%3dScience%253EAustralian%252BCurriculum%26template%3ddomSecondary%26ed%3dsite%2fseced31.nsf", 
"Skin": "OneStopScience%20Green"}, 
"tags": [ 
    "s_science"' 
    "s_maths"' 
    "s_arts" 
], 
{"uid": "5",}]}) 

然後您將它們引用爲data.tags[0], data.tags[1], data.tags[2]

0

如果你的反應是這種格式

YourResponse = { 
    "products" : [ 
        {"uid" :"5", ......., "whtever":"someval"}, 
        {"uid" :"6", ......., "whtever":"someval1"} 
       ] 

}; 

您可以使用此

$(YourResponse).each(
function(objName, objValue) { 
    console.log(objName); // wil get object name like uid, whtever 
    console.log(objValue); // wil get object's value 

}); 

所以要得到標籤,你將不得不採取團的建議;發送他們在陣列

相關問題