Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON object in C#

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#

like image 936
Rahul Rajput Avatar asked Mar 26 '13 10:03

Rahul Rajput


People also ask

Can you use JSON in C?

Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.

How do I parse a JSON file?

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.

What is JSON parse () method?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is JSON parsing example?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.


2 Answers

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);
like image 120
Sachin Avatar answered Oct 15 '22 08:10

Sachin


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);
like image 45
Ravi Gadag Avatar answered Oct 15 '22 08:10

Ravi Gadag