5

I am getting following JSON data

[{"id":"1","text":"System Admin","target":{"jQuery1710835279177001846":12},"checked":true,"state":"open"},
{"id":"2","text":"HRMS","target":{"jQuery1710835279177001846":34},"checked":false,"state":"open"},
{"id":"3","text":"SDBMS","target":{"jQuery1710835279177001846":42},"checked":false},
{"id":"8","text":"Admin","target":{"jQuery1710835279177001846":43},"checked":false},
{"id":"9","text":"My test Admin","target":{"jQuery1710835279177001846":44},"checked":false,"state":"open"},
{"id":"24","text":"ModuleName","target":{"jQuery1710835279177001846":46},"checked":false,"state":"open"}]

which try to parsed using Json.Net using strongly type

this are my property class

public class testclass
    {
        public string id { get; set; }
        public string text { get; set; }
        public string @checked { get; set; }
        public string state { get; set; }
        public target jQuery1710835279177001846 { get; set; }

    }
    public class testclass2
    {
        public List<testclass> testclass1 { get; set; }

    }

    public class target
    {
        public string jQuery1710835279177001846 { get; set; }
    }

and here i am trying to access the data i am getting exception

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuexstERP.Web.UI.Areas.SysAdmin.Controllers.testclass' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

My controller code look like

 public void Test(string Name, object modeldata)
        {

            var obj = JsonConvert.DeserializeObject<testclass>(Name);

        }

Any idea how to solve this issue in C#

3 Answers 3

8

Your Json string looks to have serialized array object in it because it contains [ ]. It means you have a Json string which is formed after serialization of array object. So you need to deserialized into array object, so try this

var obj = JsonConvert.DeserializeObject<List<testclass>>(jsonString);
Sign up to request clarification or add additional context in comments.

Comments

3

you have Array of TestClass. so it should be like this.

var model= JsonConvert.DeserializeObject<List<testclass>>(Name);

why you are using JSonConvert ? in MVC3 you can do like this

return Json(yourmodel,JsonRequestBehavior.AllowGet);

1 Comment

i want to get data from view to server side
1

Your json objects are like this

{
      "id":"1",
      "text":"System Admin",
      "target":{
         "jQuery1710835279177001846":12
      },
      "checked":true,
      "state":"open"
}

It should be like this I guess

{
      "id":"1",
      "text":"System Admin",
      "jQuery1710835279177001846":12,
      "checked":true,
      "state":"open"
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.