C# ASP.NET Web 服务 - 尝试反序列化 JSON 并获取空对象
好吧,我知道我一定错过了一些简单的东西。
感谢您的帮助!
[编辑澄清]
我遇到的问题是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 JSON 字符串表示一个具有名为
ResultSet
的属性的对象,其中包含嵌套数据。之间的区别:
请注意 JSON 序列化 ResultSet和包含 ResultSet 的 JSON 序列化 对象
换句话说,如果省略
ResultSet 来自输入字符串:
或者,如果您将字符串反序列化为具有名为
ResultSet
的属性的类:在这种情况下,您将需要使用:
请注意
[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:
And a JSON serialized object which contains a ResultSet:
In other words, it will work if you omit
ResultSet
from the input string:Or, if you deserialize the string into a class which has a property named
ResultSet
:In which case you will need to use:
Note that
[Serializable]
attribute has nothing to do with XML serialization, and is not needed. To control the output of theXmlSerializer
, use attributes from the System.Xml.Serialization namespace.尝试在 webmethod 下添加 [ScriptMethod(ResponseFormat = ResponseFormat.Json)],如下所示:
try adding [ScriptMethod(ResponseFormat = ResponseFormat.Json)] under the webmethod like such:
您的 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.