Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deserialize JSON

I'm working on project in which I'm Posting data from asp.net webform to WCF service. I'm posting data through params and the service respond me back a JSON string. Now I have an issue in deserialize. I read many threads but didn't find any solution. Hope someone can sort out my problem. Thanks in Advance

Response from WCF

{"LoginResult":false}

I just want "false" value.

How I tried:

    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>();
like image 453
M A. Avatar asked Feb 03 '14 14:02

M A.


People also ask

How does JSON deserialize work?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.

What is JSON deserialization in Java?

Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished. Gson fromJson() Example. import java.

What is the difference between serialize and deserialize?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. The byte stream created is platform independent.


2 Answers

You could use json.net to do it like this:

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

var deserializedResponse = JsonConvert.DeserializeObject<AuthResponse>(strResult);

http://james.newtonking.com/json

like image 156
hutchonoid Avatar answered Oct 14 '22 00:10

hutchonoid


.Net 4.5 has a JavaScriptSerializer, which should work as well:

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);

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx

like image 2
artm Avatar answered Oct 14 '22 00:10

artm