C# ASP.NET Web 服务 - 尝试反序列化 JSON 并获取空对象

发布于 2024-11-06 16:52:23 字数 2584 浏览 3 评论 0原文

好吧,我知道我一定错过了一些简单的东西。

感谢您的帮助!

[编辑澄清]

我遇到的问题是 JSON 输入没有被正确反序列化或其他什么,并且给了我一个空的 XML 结果。我想要 XML 格式的输出,我只是不希望它为空。

该代码是我的实际代码的简化版本。在我的真实代码中,我从另一个网站检索 JSON,并尝试解析它并在 XML Soap 请求中返回它。

为了简单起见,我以 JSON 字符串为例,对其进行了简单的硬编码。

[WebService(Namespace="my.soap")]
public class StockQuote : WebService
{

  [WebMethod(Description="",EnableSession=false)]
  public ResultSet IBM()
  {
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = "{\"ResultSet\":{\"Query\":\"ibm\",\"Result\":[{\"symbol\":\"IBM\",\"name\": \"International Business Machines Corp.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.F\",\"name\": \"IBM\",\"exch\": \"FRA\",\"type\": \"S\",\"exchDisp\":\"Frankfurt\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.DE\",\"name\": \"IBM\",\"exch\": \"GER\",\"type\": \"S\",\"exchDisp\":\"XETRA\",\"typeDisp\":\"Equity\"},{\"symbol\":\"^AXI\",\"name\": \"Stlmt ID - NASDAQ OMX Alpha IBM\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"^IVSPY\",\"name\": \"NASDAQ OMX Alpha IBM vs. SPY\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"IBMSX\",\"name\": \"Invesco Multi-Sector B\",\"exch\": \"NAS\",\"type\": \"M\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Fund\"},{\"symbol\":\"IBM.BE\",\"name\": \"IBM\",\"exch\": \"BER\",\"type\": \"S\",\"exchDisp\":\"Berlin\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.SG\",\"name\": \"IBM\",\"exch\": \"STU\",\"type\": \"S\",\"exchDisp\":\"Stuttgart\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.BA\",\"name\": \"International Business Machines Corp.\",\"exch\": \"BUE\",\"type\": \"S\",\"exchDisp\":\"Buenos Aires\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.L\",\"name\": \"International Business Machines Corp.\",\"exch\": \"LSE\",\"type\": \"S\",\"exchDisp\":\"London\",\"typeDisp\":\"Equity\"}]}}";
    return serializer.Deserialize<ResultSet>(json);
  }
}

[Serializable]
public class ResultSet
{
  public string Query;
  public ResSet[] Result;
}

[Serializable]
public class ResSet
{
  public string symbol;
  public string name;
  public string exch;
  public string type;
  public string exchDisp;
  public string typeDisp;
}

我收到 Web 服务返回的以下内容,而不是格式化对象:

 <?xml version="1.0" encoding="utf-8" ?> 
    <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="my.soap" /> 

Ok, I know I must be missing something simple.

Thank-you for your help!

[EDITED to clarify]

The problem I'm having is that the JSON input is not being deserialized properly or something and is giving me an empty XML result. I want the output in XML format, I just don't want it to be blank.

The code is a simplified version of my actual code. In my real code I'm retreiving the JSON from another website, and I'm trying to parse it and return it in an XML soap request.

To simplify things, I took the JSON string and simply hard coded it as an example.

[WebService(Namespace="my.soap")]
public class StockQuote : WebService
{

  [WebMethod(Description="",EnableSession=false)]
  public ResultSet IBM()
  {
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = "{\"ResultSet\":{\"Query\":\"ibm\",\"Result\":[{\"symbol\":\"IBM\",\"name\": \"International Business Machines Corp.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.F\",\"name\": \"IBM\",\"exch\": \"FRA\",\"type\": \"S\",\"exchDisp\":\"Frankfurt\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.DE\",\"name\": \"IBM\",\"exch\": \"GER\",\"type\": \"S\",\"exchDisp\":\"XETRA\",\"typeDisp\":\"Equity\"},{\"symbol\":\"^AXI\",\"name\": \"Stlmt ID - NASDAQ OMX Alpha IBM\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"^IVSPY\",\"name\": \"NASDAQ OMX Alpha IBM vs. SPY\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"IBMSX\",\"name\": \"Invesco Multi-Sector B\",\"exch\": \"NAS\",\"type\": \"M\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Fund\"},{\"symbol\":\"IBM.BE\",\"name\": \"IBM\",\"exch\": \"BER\",\"type\": \"S\",\"exchDisp\":\"Berlin\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.SG\",\"name\": \"IBM\",\"exch\": \"STU\",\"type\": \"S\",\"exchDisp\":\"Stuttgart\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.BA\",\"name\": \"International Business Machines Corp.\",\"exch\": \"BUE\",\"type\": \"S\",\"exchDisp\":\"Buenos Aires\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.L\",\"name\": \"International Business Machines Corp.\",\"exch\": \"LSE\",\"type\": \"S\",\"exchDisp\":\"London\",\"typeDisp\":\"Equity\"}]}}";
    return serializer.Deserialize<ResultSet>(json);
  }
}

[Serializable]
public class ResultSet
{
  public string Query;
  public ResSet[] Result;
}

[Serializable]
public class ResSet
{
  public string symbol;
  public string name;
  public string exch;
  public string type;
  public string exchDisp;
  public string typeDisp;
}

I'm getting the following returned by the web service instead of the formatted object:

 <?xml version="1.0" encoding="utf-8" ?> 
    <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="my.soap" /> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的韩爷 2024-11-13 16:52:23

您的 JSON 字符串表示一个具有名为 ResultSet 的属性的对象,其中包含嵌套数据。

之间的区别:

{
   "Query": "ibm",
   "Result": [ ... ]
}

请注意 JSON 序列化 ResultSet和包含 ResultSet 的 JSON 序列化 对象

{
   "ResultSet":
   {
      "Query": "ibm",
      "Result": [ ... ]
   }
}

换句话说,如果省略 ResultSet 来自输入字符串:

string json = @"{"Query":"ibm","Result":[ ... ]}";

或者,如果您将字符串反序列化为具有名为 ResultSet 的属性的类:

public class ResultSetWrapper
{
    public ResultSet ResultSet;
}

在这种情况下,您将需要使用:

return serializer.Deserialize<ResultSetWrapper>(json);

请注意 [Serializable] 属性与 XML 序列化无关,并且不需要。要控制 XmlSerializer 的输出,请使用 System.Xml.Serialization 命名空间。

Your JSON string represents an object with a property named ResultSet, containing nested data.

Note the difference between a JSON serialized ResultSet:

{
   "Query": "ibm",
   "Result": [ ... ]
}

And a JSON serialized object which contains a ResultSet:

{
   "ResultSet":
   {
      "Query": "ibm",
      "Result": [ ... ]
   }
}

In other words, it will work if you omit ResultSet from the input string:

string json = @"{"Query":"ibm","Result":[ ... ]}";

Or, if you deserialize the string into a class which has a property named ResultSet:

public class ResultSetWrapper
{
    public ResultSet ResultSet;
}

In which case you will need to use:

return serializer.Deserialize<ResultSetWrapper>(json);

Note that [Serializable] attribute has nothing to do with XML serialization, and is not needed. To control the output of the XmlSerializer, use attributes from the System.Xml.Serialization namespace.

等风来 2024-11-13 16:52:23

尝试在 webmethod 下添加 [ScriptMethod(ResponseFormat = ResponseFormat.Json)],如下所示:

[WebMethod(Description="",EnableSession=false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

try adding [ScriptMethod(ResponseFormat = ResponseFormat.Json)] under the webmethod like such:

[WebMethod(Description="",EnableSession=false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
骄兵必败 2024-11-13 16:52:23

您的 Web 服务尚未配置为输出 JSON,它已设置为 XML。请参阅此 SO 帖子了解如何设置它。

Your web service has not been configured to output JSON, it's setup for XML. See this SO post on how to set it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文