【发布时间】:2014-09-22 10:11:39
【问题描述】:
我正在尝试将 JSON 解析为一个对象,但它一直失败。
我不断收到错误:
命名空间“System.Runtime.Serialization.Json”中不存在类型或命名空间名称“DataContractJsonSerializer”(您是否缺少程序集引用?)
但是到目前为止,所有的引用都已添加,但不知何故它仍然无法正常工作。
我在这里做对了吗?
我目前的代码如下:
public Json_Connection ()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create (
"http://10.190.80.248/WebService/webservice.asmx/getStudentID?id=1");
// If required by the server, set the credentials.
request.ContentType = "application/json";
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams and the response.
XmlSerializer serializer = new DataContractJsonSerializer (Json.GetType(), new Type[] { typeof(Json) });
Json flippo = (Json)serializer.Deserialize (reader);
}
我使用的是 MAC,所以这台 mac 上没有 windows 文件夹或 .net
【问题讨论】: