C# 如何反序列化JSON

C# 如何反序列化JSON,c#,asp.net,json,deserialization,webclient,C#,Asp.net,Json,Deserialization,Webclient,我正在从事一个项目,在该项目中,我将数据从asp.net webform发布到WCF服务。我通过params发布数据,服务会回复我一个JSON字符串。现在我在反序列化中遇到了一个问题。我读了很多文章,但没有找到任何解决办法。希望有人能解决我的问题。提前谢谢 来自WCF的响应 {“LoginResult”:false} 我只想要“false”值 我是如何尝试的: string URL = "http://localhost:32319/ServiceEmployeeLogin.svc";

我正在从事一个项目,在该项目中,我将数据从asp.net webform发布到WCF服务。我通过params发布数据,服务会回复我一个
JSON字符串
。现在我在反序列化中遇到了一个问题。我读了很多文章,但没有找到任何解决办法。希望有人能解决我的问题。提前谢谢

来自WCF的响应

{“LoginResult”:false}

我只想要
“false”

我是如何尝试的:

    string URL = "http://localhost:32319/ServiceEmployeeLogin.svc"; 
    WebRequest wrGETURL;
    wrGETURL = WebRequest.Create(URL+"/"+emp_username+"/"+emp_password+"/"+emp_type);
    wrGETURL.Method = "POST";
    wrGETURL.ContentType = @"application/json; charset=utf-8";
    HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;

    Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
    // read response stream from response object
    StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);

    // read string from stream data
    strResult = loResponseStream.ReadToEnd();

    var jObj = JObject.Parse(strResult);
    var dict = jObj["LoginResult"].Children().Cast<JProperty>();
stringurl=”http://localhost:32319/ServiceEmployeeLogin.svc"; 
WebRequest-wrGETURL;
wrGETURL=WebRequest.Create(URL+“/”+emp_用户名+“/”+emp_密码+“/”+emp_类型);
wrGETURL.Method=“POST”;
wrGETURL.ContentType=@“应用程序/json;字符集=utf-8”;
HttpWebResponse webresponse=wrGETURL.GetResponse()作为HttpWebResponse;
Encoding enc=System.Text.Encoding.GetEncoding(“utf-8”);
//从响应对象读取响应流
StreamReader loResponseStream=新的StreamReader(webresponse.GetResponseStream(),enc);
//从流数据中读取字符串
strResult=loResponseStream.ReadToEnd();
var jObj=JObject.Parse(strResult);
var dict=jObj[“LoginResult”].Children().Cast();

您可以使用json.net这样做:

public class AuthResponse {
    public bool LoginResult { get; set; }
}

var deserializedResponse = JsonConvert.DeserializeObject<AuthResponse>(strResult);
公共类AuthResponse{
公共bool LoginResult{get;set;}
}

var deserializedResponse=JsonConvert.DeserializeObject

.Net 4.5有一个JavaScriptSerializer,它也可以工作:

public class AuthResponse {
    public bool LoginResult { get; set; }
}

System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();

AuthResponse response = sr.Deserialize<AuthResponse>(responseText);
公共类AuthResponse{
公共bool LoginResult{get;set;}
}
System.Web.Script.Serialization.JavaScriptSerializer sr=新的System.Web.Script.Serialization.JavaScriptSerializer();

AuthResponse=sr.反序列化

宾果谢谢@hutchonoid