【问题标题】:How to return JSON from webservice如何从 web 服务返回 JSON
【发布时间】:2012-07-11 22:47:32
【问题描述】:

早上,

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

[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 + "\"}"

【问题讨论】:

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


【解决方案1】:

用途:

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

【讨论】:

  • 谢谢,但如上所述我仍然得到 XML 字符串
  • 实际上它在 XML 中返回 Json。您需要在通话中指定要返回的内容。为什么它以格式返回? haacked.com/archive/2009/06/25/json-hijacking.aspx
  • 重新看答案。
  • 是的,你可以看看,stackoverflow.com/questions/1121559/…
  • arr 我明白了,只是在我的应用程序中进行了测试,而不是在我的实际 Web 服务及其返回的 json 中进行了测试。只需要格式化它,所以我有我想要返回的内容:) 如果我像使用 responseText 那样添加一个布尔值来传递一个真/假,我会正确假设吗?
【解决方案2】:

请为您的网络方法使用该属性

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

调用者将他的内容类型设置为 application/json 以使用 web 方法

【讨论】:

  • 不起作用,仍然只能返回一个字符串 - tempuri.org/">It 正常工作
【解决方案3】:

试试这个:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      这是我对框架 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());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-22
        • 1970-01-01
        • 2019-05-24
        • 2019-07-25
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 2013-05-25
        相关资源
        最近更新 更多