如何从json获取数据

时间:2011-09-12 08:00:47

标签: c# asp.net json

  

可能重复:
  How do i parse json?

请告诉我在asp.net c#中反序列化json数据的方法。 实际上我有一个带有两个对象的json数据,如:

{
    "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"
            }
        ]
    }
}

这里我有两个对象视频和预告片。请告诉我在我的代码中获取这些数据的过程。

2 个答案:

答案 0 :(得分:1)

您需要创建一个具有嵌套成员的类 例如json文件

{
"GeminiURL":"https://gemini.com/Gemini"
,"Language":"en"
,"Log":{"Debug":true,"Info":true,"Warn":true,"FileName":"d:\\temp\\tfsgemini.log"}
}
使用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;
}

,反序列化代码如下:

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;
}

答案 1 :(得分:0)