如何从webservice返回JSON

时间:2012-07-12 08:07:56

标签: c# asp.net json web-services

上午,

我需要从我的网络服务返回一条消息。下面是我的代码示例,我将返回一个字符串。

[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 ;
    }

我目前得到以下回复......

<string xmlns="http://tempuri.org/"/>

我希望返回类似

的内容
 {"success" : true, "message" : "***Message Here***"}

我相信一旦我理解了它,我将能够在需要时返回其他物品。它只是这个基础我需要解决。

非常感谢所有帮助,提前感谢:)

更新:刚发现这个......

 return "{Message:'hello world'}"

我需要像

这样的东西
 responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"

5 个答案:

答案 0 :(得分:11)

使用:

[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 ;
    }

返回的结果如下:

<string xmlns="http://tempuri.org/"/>
 {"success" : true, "message" : "***Message Here***"}
</string>

答案 1 :(得分:2)

请使用webmethod的属性

   [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

调用者将其contenttype设置为application / json以使用webmethod

答案 2 :(得分:0)

试试这个:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format. 
public bool addUser(UserModel um)
    {
        bool result = false;
        result = Conversion.intToBool(SplashAwardsDB.executeNonQuery(
            "INSERT INTO dbo.User ("
            + "userName, password, firstName, lastName, address, contactNo, birthDate, familyID, familyRole, x, y ) "
            + " VALUES ("
            + "'" + um.userName + "', "
            + "'" + um.password + "', "
            + "'" + um.firstName + "', "
            + "'" + um.lastName + "', "
            + "'" + um.address + "', "
            + "'" + um.contactNo + "', "
            + "'" + um.birthDate + "', "
            + "'" + um.familyID + "', "
            + "'" + um.familyRole + "', "
            + "'" + um.x + "', "
            + "'" + um.y + "')"
            ));
        return result;
    }

答案 3 :(得分:0)

要删除服务响应中的XML标记,请参阅StackOverflow上的以下答案:

ASP.NET WebService is Wrapping my JSON response with XML tags

答案 4 :(得分:0)

这是我对framewok 4.5.2的解决方案, 在FilterConfig类中添加以下代码, 注意:您将需要lib Newtonsoft。

 public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        GlobalConfiguration.Configuration.EnableCors();
        filters.Add(new HandleErrorAttribute());
    }
}