Reputation: 637
Is there is any other way deserialize JSON string,rather than using the Newtonsoft library? I've a string like
string json = "{status : '1',message : '<tr><th>Date</th><th>Description</th><th>Reference</th> <th>Code</th><th>Dept Code</th><th>Debit</th><th>Credit</th></tr>'}";
if i want to access the message property in code behind file, how can i do that?
Upvotes: 1
Views: 3937
Reputation: 19217
Consider this:
You need this required namespaces:
using System.Web.Script.Serialization;
Consider this class:
[Serializable]
public class Foo
{
public int status { get; set; }
public string message { get; set; }
}
SerializableAttribute
is required to work with JavaScriptSerializer
USAGE
JavaScriptSerializer serializer = new JavaScriptSerializer();
// Deserialize
Foo foo = serializer.Deserialize<Foo>(json);
//now you have access to...
var status = foo.status;
var message = foo.message;
You may also deserialize with JavaScriptSerializer
in a Dictionary
. See this:
Dictionary<string, object> ds = serializer .Deserialize<Dictionary<string, object>>(json);
var status = ds["status"].ToString();
var message = ds["message"].ToString();
Upvotes: 2
Reputation: 17485
Create class
public class TestM
{
public string status { get; set; }
public string message { get; set; }
}
Than use this in your code
JavaScriptSerializer ser = new JavaScriptSerializer();
TestM t = ser.Deserialize<TestM>("{status : '1',message : '<tr><th>Date</th><th>Description</th><th>Reference</th> <th>Code</th><th>Dept Code</th><th>Debit</th><th>Credit</th></tr>'}");
Upvotes: 0
Reputation: 56566
You could use the DataContractJsonSerializer
. Deserialize it as a class with what you want to extract, e.g.
[DataContract]
public class Message
{
[DataMember]
public string message { get; set; }
}
Upvotes: 4