开发者

C# ASP.NET web service - Trying to deserialize JSON and getting an empty object

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\",\"e开发者_如何转开发xch\": \"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" /> 


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.


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜