Random Musings

July 4, 2009

Json.NET and json.js

Filed under: c# — haditeo @ 11:45 am
Tags: ,

Let’s say i have an “Argument” class and “Customer” class below :

    public class Argument
    {
        private string _filterComboType;

        public string FilterComboType
        {
            get { return _filterComboType; }
            set { _filterComboType = value; }
        }

        private string _selectedNameClientId;
        
        public string SelectedNameClientId
        {
            get {return _selectedNameClientId;}
            set {_selectedNameClientId = value;}
        }

        private List<Customer> customerList;

        public List<Customer> Customer
        {
            get { return customerList; }
            set { customerList = value; }
        }
        
        public Argument()
        {
        
        }
    }
    public class Customer
    {
        private string _customerType;
        private string _customerText;
        private string _customerValue;

        public string ObjectType
        {
            get { return _customerType; }
            set { _customerType = value; }
        }

        public string CustomerText
        {
            get { return _customerText; }
            set { _customerText = value; }
        }

        public string CustomerValue
        {
            get { return _customerValue; }
            set { _customerValue = value; }
        }

        public Customer(string strCustomerType, string strCustomerText, string strCustomerValue)
        {
            _customerType = strCustomerType;
            _customerText = strCustomerText;
            _customerValue = strCustomerValue;
        }
        
        public Customer()
        {
        
        }
    }

The purpose of these objects is to be passed along from the “main” page to the “lookup” page to be processed further.

Refer to the sample result of these objects on the JSON string format for one Customer

{"FilterComboType":"NameOfClient","SelectedNameClientId":null,"Customer":[{"ObjectType":"account","CustomerText":"Account E","CustomerValue":"c0b377cf-1766-de11-8f5c-0003ffb4d763"}]}

and here is the sample JSON string result for multiple Customers

{"FilterComboType":"ContactList","SelectedNameClientId":null,"Customer":[{"ObjectType":"contact","CustomerText":"Lim","CustomerValue":"e0203ac0-2a3d-de11-8641-0003ffc4d675"},{"ObjectType":"contact","CustomerText":"Contact B","CustomerValue":"a0ff10e6-1766-de11-8f5c-0003ffb4d763"},{"ObjectType":"contact","CustomerText":"Contact C","CustomerValue":"9088e0ec-1766-de11-8f5c-0003ffb4d763"}]}

What are the advantages of creating JSON string format ?

you can access those objects at the client side easily, in example :

function AccessArgument(arg)
{
  var jsonStringArgument = arg;
  var argument = eval(jsonStringArgument);

  var filterComboTypeValue = argument.FilterComboType;
  
  for(var i=0; i<argument.Customer.length; i++)
  {
    var customerValue = argument.Customer&#91;i&#93;.CustomerValue;
  }
}

&#91;/sourcecode&#93;

Notice how easily i'm accessing a list of the argument's Customer in array object at the client side

How do you convert from JSON string to the actual object itself ?

&#91;sourcecode='jscript'&#93;
var objectArgument = eval('a Json String');
&#91;/sourcecode&#93;

How do you convert from actual object back to the JSON string ? You need <a href="http://www.json.org/js.html">json2.js</a> which can be downloaded <a href="http://www.json.org/js.html">here</a> . The javascript file needs to be included in the html head.


var jsonText = JSON.stringify(objectArgument);

I like to use Newtonsoft JSON.NET to serialize my C# object to the JSON string format as well as deserialize my C# object back to the JSON string format

Here are the code sample how to serialize the Argument object into JSON string

txtAccountListJson.Text = Newtonsoft.Json.JavaScriptConvert.SerializeObject(newArg);

And here are the code sample in how to serialize the JSON string back to the actual object

Argument arg = (Argument)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(txtContactListJson.Text, typeof(Argument));

Blog at WordPress.com.