locked
How to convert json object to generic list. RRS feed

  • Question

  • Hi all,

    How to convert json object to generic list.

    I received wcf rest service like a  below format.

    {"StudentList ":{"name":"abc"},{"age":"21"}}

    I want to assign it to generic list. I am using windows phone 8 application in visual studio 2012.

    Thanks

    Friday, February 7, 2014 10:28 AM

Answers

  • Make a class that will represent your json object. I use json2csharp.com website for that. I also use json.net(Newtonsoft.json) like mentioned above to deserialize json. You can get that from NuGet.

    then after you make your class you can make a List of that class. So for example List<StudentListClass>.

    then after you get your string from the wcf you use json.net to do the rest of the work. 

    (example code, not tested)

    //this is the list

    List<StudentListClass> list = new List<StudentListClass>();

    //lets imagine this string contained your json string

    String JsonString;

    //now we deserialize it.

    //im not sure how your data entirely looks so ill put two ways that might work depending on your json

    list = JsonConvert.DeserializeObject<List<StudentListClass.RootObject>>(JsonString)

    OR

    StudentListClass.RootObject result = (StudentListClass.RootObject)JsonConvert.DeserializeObject(JsonString, typeof(StudentListClass.RootObject));

    hope this helped.

    Friday, February 7, 2014 11:02 AM
  • Hello!

    I use Newtonsoft.Json package for this. It is very helpful.


    Friday, February 7, 2014 10:51 AM
  • Do not forget you can use paste json as classes to make the class you can deserialize to 

    http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx

    Friday, February 7, 2014 1:12 PM

All replies

  • Hello!

    I use Newtonsoft.Json package for this. It is very helpful.


    Friday, February 7, 2014 10:51 AM
  • Make a class that will represent your json object. I use json2csharp.com website for that. I also use json.net(Newtonsoft.json) like mentioned above to deserialize json. You can get that from NuGet.

    then after you make your class you can make a List of that class. So for example List<StudentListClass>.

    then after you get your string from the wcf you use json.net to do the rest of the work. 

    (example code, not tested)

    //this is the list

    List<StudentListClass> list = new List<StudentListClass>();

    //lets imagine this string contained your json string

    String JsonString;

    //now we deserialize it.

    //im not sure how your data entirely looks so ill put two ways that might work depending on your json

    list = JsonConvert.DeserializeObject<List<StudentListClass.RootObject>>(JsonString)

    OR

    StudentListClass.RootObject result = (StudentListClass.RootObject)JsonConvert.DeserializeObject(JsonString, typeof(StudentListClass.RootObject));

    hope this helped.

    Friday, February 7, 2014 11:02 AM
  • Do not forget you can use paste json as classes to make the class you can deserialize to 

    http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx

    Friday, February 7, 2014 1:12 PM