如何从web服务返回JSON [英] How to return JSON from webservice
本文介绍了如何从web服务返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
上午,
我需要从我的web服务返回的消息。下面是我的code的样本,我返回一个字符串。
[Web方法]
公共字符串CheckFeedSubmission()
{
字符串的responseText =;
尝试
{
//东西放在这里
的responseText =成功了!
}
赶上(例外前){responseText的=哎呀wehave错误异常消息:+ ex.Message; }
返回的responseText;
}
我目前得到以下响应...
<字符串的xmlns =http://tempuri.org//>
我想最好想返回类似
{成功:真实的,消息:***讯息:***}
我敢肯定,一旦我得到它的想法,我将能够在必要时返回其他物品。它只是这个基地我需要锻炼。
所有帮助是非常AP preciated,在此先感谢:)
更新:刚刚发现这...
回归{消息:世界你好'}
我会需要这样的东西。
的responseText ={成功:真实的,消息:\\有一个错误。消息:+ ex.Message +\\}
解决方案
使用:
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] //指定返回格式。
公共字符串CheckFeedSubmission()
{
字符串的responseText =;
尝试
{
//东西放在这里
的responseText =成功了!
}
赶上(例外前){responseText的=哎呀wehave错误异常消息:+ ex.Message; }
返回的responseText;
}
返回的将是类似的结果:
<字符串的xmlns =http://tempuri.org//>
{成功:真实的,消息:***讯息:***}
< /串>
Morning,
I need to return a message from my web service. Below is a sample of my code, and i am returning a string.
[web method]
public string CheckFeedSubmission()
{
string responseText = "";
try
{
//Stuff goes here
responseText = "It Worked!"
}
catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
return responseText ;
}
I currently get the following response...
<string xmlns="http://tempuri.org/"/>
I would ideally like to return something like
{"success" : true, "message" : "***Message Here***"}
I am sure once i get the idea of it, i will be able to return other items if needed. Its just this base i need to work out.
All help is much appreciated, thanks in advance :)
UPDATE: Just found this...
return "{Message:'hello world'}"
Would i need something like
responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"
解决方案
Use:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format.
public string CheckFeedSubmission()
{
string responseText = "";
try
{
//Stuff goes here
responseText = "It Worked!"
}
catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
return responseText ;
}
The result returned will be like:
<string xmlns="http://tempuri.org/"/>
{"success" : true, "message" : "***Message Here***"}
</string>
这篇关于如何从web服务返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文