1

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?

gdoron
151k59 gold badges302 silver badges374 bronze badges
asked Dec 20, 2011 at 16:19

3 Answers 3

4

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; }
}
answered Dec 20, 2011 at 16:23

2 Comments

Which namespace do i need to use for that?thanks for your prompt answer
DataContractJsonSerializer is in System.Runtime.Serialization.Json. DataContractAttribute and DataMemberAttribute are in System.Runtime.Serialization. The Message class can be in the namespace of your choosing.
2

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();
answered Dec 20, 2011 at 16:27

4 Comments

Which namespace do i need to call for this. thanks for your reply
Added namespace on the answer, please check
i got an error when passing a table data as message property. Invalid object passed in, ':' or '}' expected. (37): {status : 1 , message : '<table id='excelDoc'><tr><th>Date</th><th>Description</th><th>Reference</th><th>Nominal Code</th><th>Dept Code</th><th>Debit</th><th>Credit</th></tr><tr><td>21/11/2011</td><td>Nov depreciation </td><td>Dep jnl</td><td></td><td></td><td class='db'></td><td class='cr'></td></tr><tr><td></td><td></td><td></td><td>1150</td><td>100</td><td class='db'>0.00</td><td class='cr'>50.00</td></tr></table>'}
You can't use ' within a string, it must be escaped with \'
0
  1. Create class

     public class TestM
     {
     public string status { get; set; }
     public string message { get; set; }
     }
    
  2. 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>'}");
    
Abdul Munim
19.2k8 gold badges54 silver badges61 bronze badges
answered Dec 20, 2011 at 16:26

3 Comments

thanks for your reply it is possible to use anonymous object.
var TestM = new { status = string.Empty, message = string.Empty };
anonymous object.. i am not quite sure

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.