1

I have one concrete class called ShipFromAddress and where I am deserializing my json this below way

JavaScriptSerializer jss = new JavaScriptSerializer();
oShipFromAddress = jss.Deserialize<ShipFromAddress>(Request.Cookies["ShipFromAddress"].Value);

Concrete class

public class ShipFromAddress
{
    public string Weight
    { 
        get; 
        set; 
    }

    public string addressLine1
    {
        get;
        set;
    }

    public string addressLine2
    {
        get;
        set;
    }

    public string city
    {
        get;
        set;
    }

    public string postcode
    {
        get;
        set;
    }

    public string countrycode
    {
        get;
        set;
    }

    public string StateCode
    {
        get;
        set;
    }
}

I do not want to create or use concrete class rather I want to do that deserialization on the fly with the help of dynamic object or anonymous class concept. Please guide me with sample code.

i got two solution.....which looks good

1) when need to pass multiple data serialize to anonymous the example would be

var query = from employee in employees select new { Name = employee.Name, Id = employee.Id };
LogEmployees(query);

public void LogEmployees (IEnumerable<dynamic> list)
{
    foreach (dynamic item in list)
    {
        string name = item.Name;
        int id = item.Id;
    }
}

method argument type must be IEnumerable<dynamic> because LogEmployees() function expecting multiple data

2) when passing single data the code look like

public class Program
{
    private static void Thing(dynamic other)
    {
        Console.WriteLine(other.TheThing);
    }

    private static void Main()
    {
        var things = new { TheThing = "Worked!" };
        Thing(things);
    }
}
2

2 Answers 2

4

With JavaScriptSerializer you can use the DeserializeObject method from serializer which will return just an object:

JavaScriptSerializer jss = new JavaScriptSerializer();
object obj= jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);

Internally it will be represented as a Dictionary<string, object>, so you can cast it to it and use like this:

var values = (Dictionary<string, object>)jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();

Or you can cast it to dynamic:

dynamic values = jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();

Alternatively, You can use Json.NET library and it's JsonConvert class (benchmarks show that it performs faster than JavaScriptSerializer). The code will look like this:

dynamic values = JsonConvert.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values.addressLine1;
Sign up to request clarification or add additional context in comments.

6 Comments

I think the OP wants a strongly typed object.
you might want to declare obj as dynamic, which is more useful here, and in general JavaScriptSerializer is a bad idea - use JsonConvert from Json.NET - that's faster and less buggy.
@EamonNerbonne added a note regarding Json.NET
@YuvalItzchakov OP states that he wants to deserialize the object into dynamic structure, not a concrete class
dynamic as the dynamic type?
|
3

I use Newtonsoft.Json

string source = Request.Cookies["ShipFromAddress"].Value as string;

var address = JsonConvert.DeserializeObject<ShipFromAddress>(source);

2 Comments

address1,address2,city, postcode etc all will be stored in address variable? can we access like address.addressLine1 or address.City ?
you can access it like as an objecto of the class ShipFromAddress address.City , address.addressLine1 , etc

Your Answer

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.