Tuesday, December 17, 2013

How to Convert JSON XML string representation to Object in C#

my workmate had asked me this question for while.. I gave him the solution is that he did not need to convert the JSon object to C#object. he can create a new store procedure to load the data into the C# object.

currently i worked on a project that i need to consume the JSon object via the WCF  (Windows communication foundation). Since I did not have the control on the data source, it means that i can call the store procedure to load those data into the C# object.

JSON.Net is the solution to deserialzie the json object back to .net object.


http://james.newtonking.com/json

http://json.codeplex.com/

First we will define C# object with Properties matching to the json object.

here is the json sample object from
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html

{
 "id": "0001",
 "type": "donut",
 "name": "Cake",
 "ppu": 0.55,
 "batters":
  {
   "batter":
    [
     { "id": "1001", "type": "Regular" },
     { "id": "1002", "type": "Chocolate" },
     { "id": "1003", "type": "Blueberry" },
     { "id": "1004", "type": "Devil's Food" }
    ]
  },
 "topping":
  [
   { "id": "5001", "type": "None" },
   { "id": "5002", "type": "Glazed" },
   { "id": "5005", "type": "Sugar" },
   { "id": "5007", "type": "Powdered Sugar" },
   { "id": "5006", "type": "Chocolate with Sprinkles" },
   { "id": "5003", "type": "Chocolate" },
   { "id": "5004", "type": "Maple" }
  ]
}
 
 
we define the c# object MyJson based on the JSon xml Schema

public class MyJson    {
        [JsonProperty("id")]
        public string id
        {
            get;
            set;
        }
        [JsonProperty("type")]
        public string type
        {
            get;
            set;
        }
        [JsonProperty("name")]
        public string name
        {
            get;
            set;
        }
        [JsonProperty("Pricing")]
        public Pricing Pricing
        {
            get;
            set;
        }

        [JsonProperty("ppu")]
        public string ppu
        {
            get;
            set;
        }

        [JsonProperty("batters")]
        public batters batters

        {
            get;
            set;
        }

      [JsonProperty("topping")]
        public topping[] topping

        {
            get;
            set;
        }


      

    }

    public class batters
    {
        [JsonProperty("batter")]
        public  batter[] batter
        {
            get;
            set;
        }
}

  public class batter
    {
        [JsonProperty("id")]
        public string id
        {
            get;
            set;
        }

        [JsonProperty("type")]
        public string type
        {
            get;
            set;
        }
}


  public class topping
    {
        [JsonProperty("id")]
        public string id
        {
            get;
            set;
        }

        [JsonProperty("type")]
        public string type
        {
            get;
            set;
        }
}

in my sample i will convert the json object in my page load event

 protected void Page_Load(object sender, EventArgs e)
        {
            using (var webClient = new System.Net.WebClient())
            {
                 try
                {
                    var jsonString = webClient.DownloadString("Your WCF Url with paramter input options");
                    // if you need to remove any string that did not need in the json xml string.
                    jsonString = jsonString.Replace("{\"MyJSonOjects\":", "").TrimEnd('}');
                   //use JsonConvert object in Json.Net to deserialize the json to .net Object.
                    List<MyJson> MyJsonList= JsonConvert.DeserializeObject<List<MyJson >(jsonString) as List<MyJson>;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }



       

No comments:

Post a Comment