如何从javascript或jquery中的webservice返回JSON访问数据

时间:2012-03-25 06:07:04

标签: javascript json

您好我有一个返回json数据,它返回webservice webservice的结构就是这样:

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。 请帮忙。

4 个答案:

答案 0 :(得分:1)

它是无效的json,你的标签应该是:

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

然后product.tag将是您可以成功访问的数组

此致

答案 1 :(得分:0)

如果同一对象中有多个键,则会出现未定义的行为。只保留一个,因为对不订购,你不能保证你会得到它。

简而言之:Web服务正在为您返回错误的数据。如果需要多个标记,则服务应在标记属性中返回值数组:

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

答案 2 :(得分: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]

答案 3 :(得分: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

});

所以要获得标签,你必须采取Tuan的建议;发送它们在数组