H.Kai H.Kai - 1 year ago 181
C# Question

WCF Deserialize JSON String

I had edited my question :

How can i deserialize the JSON string shows below :

"{\"acctId\": \"Test10001\",\"amount\": 200,\"currency\": \"USD\",\"Code\": \"Test\",\"serialNo\": \"1234566789asdsad0\"}"

Please give suggestion how can I get the data by using this method or any other recommended method.

Answer Source

I think below code should serve your purpose:

    public class DeserializedData
        public string acctId { get; set; }
        public string amount { get; set; }
        public string currency { get; set; }
        public string Code { get; set; }
        public string serialNo { get; set; }

StreamReader reader = new StreamReader(streamdata);
string res = reader.ReadToEnd();

Use third party dlls like Json.NET or Restsharp:

1.) Using Json.Net Json.NET

var result = JsonConvert.DeserializeObject<DeserializedData>(res);

2.) Using Restsharp Restsharp

var jsonDeserializer = new RestSharp.Deserializers.JsonDeserializer();
var response = jsonDeserializer.Deserialize<DeserializedData>(res);

Let me know if it doesn't work for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download