admin管理员组文章数量:1422007
I have a web service that returns me a JSON object that contains the string "Hello World". How do I pull this string out of the object?
data = [object Object]
Thanks
Nick
I have a web service that returns me a JSON object that contains the string "Hello World". How do I pull this string out of the object?
data = [object Object]
Thanks
Nick
Share Improve this question asked Apr 8, 2009 at 2:15 NickNick 19.7k53 gold badges190 silver badges313 bronze badges3 Answers
Reset to default 6You have to know how is your object, what members the object have.
You could try something like
for(var e in data)
alert(e + ' : ' + data[e]);
You can either use eval
:
var foo = eval('(' + data + ')');
But that is potentially dangerous, especially if you don't trust what is being sent from the server. Thus, the best way (and most secure way) to extract data from a JSON object is by using Crockford's JSON library:
var foo = JSON.parse(data);
Btw, if you're using jQuery to query ASP.Net Web Services, be careful of the the d.
issue (which is used as a container object). Thus to extract the returned object, you have to do:
var foo = JSON.parse(data);
if (foo) {
//Foo is not null
foo = f.d;
}
More information about this here: http://encosia./2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
If you are using jQuery's post function you might follow this example found here.
$.post("test.php", { func: "getNameAndTime" },
function (data) {
alert(data.name); // John
console.log(data.time); // 2pm
}, "json");
In your case, I would suspect that you would call data.data.
本文标签: aspnetPull string value out of JSON objectStack Overflow
版权声明:本文标题:asp.net - Pull string value out of JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745357911a2655161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论