-1

Possible Duplicate:
How do i parse json?

Please tell me the way to deserialize the json data in asp.net c#. Actually I have a json data with two objects like:

{
    "errstr": "All downloded vedios ",
    "errcode": 0,
    "result": {
        "videos": [
            {
                "id": "22",
                "name": "Ashley",
                "price": "0.49",
                "size": "3712310"
            }
        ],
        "trailer": [
            {
                "id": "1",
                "trailer_name": "charl1",
                "status": "1"
            },
            {
                "id": "2",
                "trailer_name": "charl2",
                "status": "1"
            }
        ]
    }
}

Here I have two objects videos and trailer. Please tell me the process to get get these data in my code.

Community
  • 1
  • 1
Rachit Gaur
  • 91
  • 1
  • 3

2 Answers2

1

you need to create a class with nested members for example json file

{
"GeminiURL":"https://gemini.com/Gemini"
,"Language":"en"
,"Log":{"Debug":true,"Info":true,"Warn":true,"FileName":"d:\\temp\\tfsgemini.log"}
}

is serializaed and deserialized with c# class

public class Settings
{
    public string GeminiURL;
    private LogSettings _log;
    public LogSettings Log
    {
        get { return _log = _log ?? new LogSettings(); }
        set { _log = value; }
    }
    public string Language;

    public Settings()
    {
        // defaule settings can be assigned here;
    }
}
public class LogSettings
{
    public bool Debug;
    public bool Info = true;
    public bool Warn = true;
    public string FileName;
}

and the deserialization code looks like:

public static T Load(string fileName)
{
    T t = new T();
        if (File.Exists(fileName))
            t = (new JavaScriptSerializer()).Deserialize<T>(File.ReadAllText(fileName));
        else
            Save(t);
    return t;
}
oleksa
  • 3,688
  • 1
  • 29
  • 54
0

JSON .NET - http://json.codeplex.com/

aku
  • 122,288
  • 32
  • 173
  • 203