3

suppose my json stored in string variable. my json look like

"{\"file\":\"dave\",\"type\":\"ward\"}"

the above data stored like in my string variable. now how can i deserialize this json data and read each component like file and type.

i tried with the below code but getting error

 var jss = new JavaScriptSerializer();
 string json = new StreamReader(context.Request.InputStream).ReadToEnd();

 var sData=jss.Deserialize<string>(json);
 string _file = sData[0].ToString();
 string _type = sData[1].ToString();

please guide my how can i extract each component like file and type after deserialize this from data in json format. thanks


Rectification

This way i solve this....here is whole client side & server side code.

     <script type="text/javascript">
     $(document).ready(function () {
         $('#btnlogin').click(function (e) {
             var FeedCrd = {};
             FeedCrd["file"] = 'dave';
             FeedCrd["type"] = 'ward';

             $.ajax({
                 type: "POST",
                 url: "CallASHX.ashx",
                 data: JSON.stringify(FeedCrd),
                 //data: { 'file': 'dave', 'type': 'ward' },
                 //dataType: "json",
                 success: function (data) {
                     if (data == "SUCCESS");
                     {
                         alert('SUCCESS');
                     }

                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     alert(textStatus);
                 }

             });
             return false;
         });
     });
 </script>

public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        var jss = new JavaScriptSerializer();
        string json = new StreamReader(context.Request.InputStream).ReadToEnd();
        Dictionary<string,string> sData=jss.Deserialize<Dictionary<string,string>>(json);
        string _file = sData["file"].ToString();
        string _type = sData["type"].ToString();
        context.Response.Write("SUCCESS");
    }
Thomas
  • 33,544
  • 126
  • 357
  • 626

3 Answers3

6

Easiest way I have found is combining the Json.NET library with the new .net dynamic object type:

string json = "{\"file\":\"dave\",\"type\":\"ward\"}";
dynamic jo = JObject.Parse(json);
string _file = jo.file;
string _type = jo.type;

Don't forget:

using Newtonsoft.Json.Linq;

This is a very simple way where you don't have to declare a strongly-typed object, therefore its simple to do on the fly.

naspinski
  • 34,020
  • 36
  • 111
  • 167
3

You should have a class defined as:

public class YourJsonClass
{
    public string file {get;set;}
    public string type {get;set;}
}

Then, you'd desrialize like this:

YourJsonClass sData=jss.Deserialize<YourJsonClass>(json);

The data will be available in the sData object, according to YourJsonClass definition, thus, sData.file will hold the file property, and so on.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1

I typically pass the JSON string to a function/method that takes a structured class as a parameter.


Javascript object

"{ q: {file:\"dave\", type:\"ward\"} }"

public class myClass
{
     private string _file = string.empty;
     public string file {
         get { return _file; }
         set { _file = value.trim(); }
       }

     private string _type = string.empty;
     public string type {
         get { return _type; }
         set { _type = value.trim(); }
       }
}

 public ClassTypeOrVariableType FunctionName(myClass q)
  {
     //do stuff here
  {
JoeFletch
  • 3,820
  • 1
  • 27
  • 36