2

this is my JSON

[{"id":23,"name":"Video Clips"},{"id":15,"name":"Deleted Scenes"},{"id":9,"name":"Music Albums"},{"id":7,"name":"Trailers"},{"id":18,"name":"Short  Films"},{"id":21,"name":"Movie Clips"},{"id":1,"name":"Movies "},{"id":4,"name":"Plays"},{"id":22,"name":"Scenes"},{"id":2,"name":"TV  Show"},{"id":5,"name":"Kids"},{"id":16,"name":"Interviews"},{"id":11,"name":"Film Songs"},{"id":14,"name":"Making of Movie"}] 

I have to deserialize it how should I do this? please help

Yahia
  • 69,653
  • 9
  • 115
  • 144
Mahendra
  • 137
  • 1
  • 2
  • 7
  • 5
    Look at [JSON.NET](http://json.codeplex.com/) – marc_s Aug 26 '11 at 11:58
  • look at this link http://stackoverflow.com/questions/7203770/how-to-deserialize-json-in-asp-net – Bobby Aug 26 '11 at 11:58
  • how to deserialize this json in c sharp – Mahendra Aug 26 '11 at 12:04
  • [{"id":23,"name":"Video Clips"},{"id":15,"name":"Deleted Scenes"},{"id":9,"name":"Music Albums"},{"id":7,"name":"Trailers"},{"id":18,"name":"Short Films"},{"id":21,"name":"Movie Clips"},{"id":1,"name":"Movies "},{"id":4,"name":"Plays"},{"id":22,"name":"Scenes"},{"id":2,"name":"TV Show"},{"id":5,"name":"Kids"},{"id":16,"name":"Interviews"},{"id":11,"name":"Film Songs"},{"id":14,"name":"Making of Movie"}] – Mahendra Aug 26 '11 at 12:07
  • http://stackoverflow.com/questions/3142495/deserialize-json-into-c-dynamic-object/3806407 – Fernando Correia Aug 26 '11 at 12:10

2 Answers2

2

The JSON you have there represents an array of objects which look like Videos so first you will need to define a class to store each video like so:

public class Video
{
    public int ID { get; set; }
    public string Name { get; set; }
}

With this done you can make use of one of the many JSON libraries either built in or third party. For this example I have made use of JSON.NET. Here is a link to the documentation.

Next you will need to make use of the DeserializeObject static generic method of the JsonConvert class like so, specifying the List<Video> type so that it knows the JSON to be de-serialized is a collection of Video objects:

using Newtonsoft.Json;

...

string json = "[{\"id\":23,\"name\":\"Video Clips\"},{\"id\":15,\"name\":\"Deleted Scenes\"},{\"id\":9,\"name\":\"Music Albums\"},{\"id\":7,\"name\":\"Trailers\"},{\"id\":18,\"name\":\"Short  Films\"},{\"id\":21,\"name\":\"Movie Clips\"},{\"id\":1,\"name\":\"Movies \"},{\"id\":4,\"name\":\"Plays\"},{\"id\":22,\"name\":\"Scenes\"},{\"id\":2,\"name\":\"TV  Show\"},{\"id\":5,\"name\":\"Kids\"},{\"id\":16,\"name\":\"Interviews\"},{\"id\":11,\"name\":\"Film Songs\"},{\"id\":14,\"name\":\"Making of Movie\"}]";

List<Video> videos = JsonConvert.DeserializeObject<List<Video>>(json);

With this done you have a collection of Video objects to work with.

Hope this helps you.

jdavies
  • 12,784
  • 3
  • 33
  • 33
1

You can deserialize the json using JavaScriptSerializer from the System.Web.Extensions dll which can be found in:

C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Web.Extensions.dll

After adding a reference to the DLL in the project add:

using System.Web.Script.Serialization;

And you will also need to set Target Framework to a non-Client Profile, for example:

.NET Framework 4

Then use code like this one to deserialize the json:

var json = @"[{""id"":23,""name"":""Video Clips""},{""id"":15,""name"":""Deleted Scenes""},{""id"":9,""name"":""Music Albums""},{""id"":7,""name"":""Trailers""},{""id"":18,""name"":""Short  Films""},{""id"":21,""name"":""Movie Clips""},{""id"":1,""name"":""Movies ""},{""id"":4,""name"":""Plays""},{""id"":22,""name"":""Scenes""},{""id"":2,""name"":""TV  Show""},{""id"":5,""name"":""Kids""},{""id"":16,""name"":""Interviews""},{""id"":11,""name"":""Film Songs""},{""id"":14,""name"":""Making of Movie""}]";
var jsonSerializer = new JavaScriptSerializer();
var deserializedList = jsonSerializer.Deserialize<List<JsonType>>(json);
Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169