3

I have receive json string in Controller, now i want to map that string in to C# class object How can i do that?

JSON:

[{"PdID":null,"StName":"435","DOB":"2015-05-02T17:09:35.974Z","Gender":"5435"},{"PdID":null,"StName":"4343","DOB":"2015-05-02T17:09:35.974Z","Gender":"4345"}]`

my class:

public class PersonDetail
{
    public int PdID { get; set; }
    public int PolicyPurchesID { get; set; }
    public string StName { get; set; }
    public DateTime DOB { get; set; }
    public byte Gender { get; set; }
}

Now in my controller i have do this:-

public ActionResult PolicyDetailAdd(string jsn)
{
    try
    {               
        JavaScriptSerializer objJavascript = new JavaScriptSerializer();

        PersonDetail testModels = (PersonDetail)objJavascript.DeserializeObject(jsn);

        return null;
     }
}

I got exception in this:

Unable to cast object of type System.Object[] to type WebApplication1.Models.PersonDetail.

How can I get this string into list object?

7
  • In a very easy way: json2csharp.com Commented May 2, 2015 at 17:29
  • but i don't want object i want class with values ...can it possible with this json2charp @PaoloCosta Commented May 2, 2015 at 17:33
  • The web site gives you the class definition, than you have to write the code to deserialize the json string Commented May 2, 2015 at 17:34
  • I have added an answer to deserialize to a specific class. Commented May 2, 2015 at 17:35
  • You could take your models collection as a parameter to your method and it will get automatically converted for you if you're sending correct json object. There's no reason why you should do it manually. So just update your method's signature to public ActionResult PolicyDetailAdd(IEnumerable<PersonDetail> data) and it will work. Commented May 2, 2015 at 17:42

2 Answers 2

3

The error occurs because you are trying to deserialize a collection to an object. Also you are using the generic Object. You will need to use

List<PersonDetail> personDetails = objJavascript.Deserialize<List<PersonDetail>>(jsn);
Sign up to request clarification or add additional context in comments.

Comments

0

You've got two issues. @Praveen Paulose is correct about the first error. But then object definitions are also incorrect relative to the JSON.

First, PdId needs to handle null:

public class PersonDetail
    {
        public int? PdID { get; set; }
        public int PolicyPurchesID { get; set; }
        public string StName { get; set; }
        public System.DateTime DOB { get; set; }
        public byte Gender { get; set; }
    }

Second, as @Praveen mentioned, the JSON is returning an array so you need to handle that.

JavaScriptSerializer objJavascript = new JavaScriptSerializer();

var testModels = objJavascript.DeserializeObject<ICollection<PersonDetail>>(jsn);

1 Comment

yes i have notice that ... and fix that thank you for Information.

Your Answer

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