最近我正在使用dailymotion视频API。但是我不知道如何将返回的数据转换为我的ASP.NET C#应用程序。
GET
https://api.dailymotion.com/videos?search=fun&page=3
RESULT
{ "page": 1,
"limit": 2,
"total": 218248,
"has_more": true,
"list": [
{ "id": "xrk9mi",
"title": "Priyanka & Shahid Kapoor get MOBBED in local train",
"channel": "fun",
"owner": "xlw7uu"
},
{ "id": "xrk8fy",
"title": "What's Up With Gaga?: Hit On Head, Perfume Bottle Leaked, Thai Fans Angry",
"channel": "music",
"owner": "xofeoz" }
]
}
答案 0 :(得分:3)
你会声明一个与你得到的东西相匹配的类,让我们把它分解成几部分,从外部类声明开始:
public class DailyMotionVideo {
public int page {get;set;}
public int limit {get;set;}
public int total {get;set;}
public bool has_more {get;set;}
public XXX[] list {get;set;}
}
所以我会对XXX做同样的事情,这需要是一个单独的类型,所以我们可以创建它们的数组:
public class DailyMotionVideoInternalList {
public string id {get;set;}
public string title {get;set;}
public string channel {get;set;}
public string owner {get;set;}
}
这要求我们返回并将该名称放入我们的第一个声明:
public class DailyMotionVideo {
public int page {get;set;}
public int limit {get;set;}
public int total {get;set;}
public bool has_more {get;set;}
public DailyMotionVideoInternalList[] list {get;set;}
}
public class DailyMotionVideoInternalList {
public string id {get;set;}
public string title {get;set;}
public string channel {get;set;}
public string owner {get;set;}
}
然后,您可以通过多种方法将接收到的对象转换为此对象,具体取决于您使用的.NET版本。
由于你已经把它作为一个字符串,我将假设该字符串被称为“结果”:
DailyMotionVideo videoList =
new JavaScriptSerializer().Deserialize<DailyMotionVideo>(result);