Click here to Skip to main content
14,920,094 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hai I have json string like this



[{"StudentId":"3","RollNo":"1"},{"StudentId":"2","RollNo":"2"},{"StudentId":"1","RollNo":"3"},{"StudentId":"4","RollNo":"4"}]

C#
public class Details
   {
       public string StudentId { get; set; }
       public string RollNo { get; set; }
   }


C#
Details result = JsonConvert.DeserializeObject<details>(str);


but error showing like this

C#
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AdmissionService+Details' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.


Can anyone help???
Posted
Updated 25-Feb-14 23:02pm
v9

Please try is as below.

List<Details> data = JsonConvert.DeserializeObject<List<Details>>(jsonString);


NOTE: I hope you're using json.NET.
   
Comments
Faisalabadians 26-Feb-14 8:17am
   
+5
as presented JSON is array then there should be an array or list of Details
Sampath Lokuge 26-Feb-14 8:19am
   
Yep,Thanks Faisalabadians. :)
Try this
C#
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Details objDetails  = jsonSerializer.Deserialize<details>(jsonInput)
</details>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900