我在JSON格式中获得了巨大的反响,我只需要2个字符串,在这里我就简化了我的JSON文件,以便更好地阅读
{
"cdn_url": "https://f.vimeocdn.com",
"vimeo_api_url": "api.vimeo.com",
"request": {
"files": {
"progressive": [
{
"profile": 164,
"width": 622,
"mime": "video/mp4",
"fps": 25,
"url": "1047326445.mp4",
"cdn": "akamai_interconnect",
"quality": "360p",
"id": 1047326445,
"origin": "gcs",
"height": 360
},
{
"profile": 165,
"width": 932,
"mime": "video/mp4",
"fps": 25,
"url": "1047326437.mp4",
"cdn": "akamai_interconnect",
"quality": "540p",
"id": 1047326437,
"origin": "gcs",
"height": 540
}
]
}
},
"video": {
"version": {
"current": null,
"available": null
},
"height": 540,
"duration": 401,
"thumbs": {
"640": "712851375_640.jpg",
"960": "712851375_960.jpg",
"base": "712851375"
},
"id": 279550927,
"default_to_hd": 0,
"url": null,
"privacy": "disable",
"unlisted_hash": null
}
}
我从中删除了许多对象,以便于阅读。 我想要“ url”:“ 1047326445.mp4”,来自“渐进数组和视频对象中“ 640”变量的字符串。
protected void btnclick_Click(object sender, EventArgs e)
{
string normalURL = "279550927";
string urlJSONcall = "https://player.vimeo.com/video/" + normalURL + "/config";
string json = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlJSONcall);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
if ((responseStream != null) && responseStream.CanRead)
{
using (var reader = new System.IO.StreamReader(responseStream))
{
json = reader.ReadToEnd();
LBresponse.Text = json;
}
}
}
finally
{
if (response != null)
{
response.Close();
}
}
var data = (JObject)JsonConvert.DeserializeObject(json);
}
}
由于嵌套对象,使我难以解决它。
我不知道下一步该怎么做,
答案 0 :(得分:0)
这与我合作:
protected void btnclick_Click(object sender, EventArgs e)
{
string normalURL = "279550927";
string urlJSONcall = "https://player.vimeo.com/video/" + normalURL + "/config";
using (var w = new WebClient())
{
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
json_data = w.DownloadString(urlJSONcall).Replace("\"640\"", "\"_640\"");;
}
catch (Exception) { }
// if string with JSON data is not empty, deserialize it to class and return its instance
var mainObject = !string.IsNullOrEmpty(json_data) ? Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json_data) : null;
string cdn_url = mainObject?.cdn_url;
string vimeo_api_url = mainObject?.vimeo_api_url;
string _640 = mainObject?.video?.thumbs?._640;
var Prgs = mainObject?.request?.files?.progressive;
foreach (var progressive in Prgs)
{
string URL = progressive.url;
}
}
}
但是,如果您关心返回的类型,则可以使用以下解决方案: 步骤1:
protected void btnclick_Click(object sender, EventArgs e)
{
string normalURL = "279550927";
string urlJSONcall = "https://player.vimeo.com/video/" + normalURL + "/config";
using (var w = new WebClient())
{
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
json_data = w.DownloadString(urlJSONcall).Replace("\"640\"", "\"_640\"");;
}
catch (Exception) { }
// if string with JSON data is not empty, deserialize it to class and return its instance
MainObject mainObject = !string.IsNullOrEmpty(json_data) ? Newtonsoft.Json.JsonConvert.DeserializeObject<MainObject>(json_data) : null;
string cdn_url = mainObject?.cdn_url;
string vimeo_api_url = mainObject?.vimeo_api_url;
string _640 = mainObject?.video?.thumbs?._640;
var Prgs = mainObject?.request?.files?.progressive;
foreach (Progressive progressive in Prgs)
{
string URL = progressive.url;
}
}
}
第二步:添加这些类以浏览属性
public class MainObject
{
public string cdn_url { get; set; }
public string vimeo_api_url { get; set; }
public Request request { get; set; }
public Video video { get; set; }
}
public class Request
{
public Files files { get; set; }
}
public class Files
{
public Progressive[] progressive { get; set; }
}
public class Progressive
{
public string url { get; set; }
}
public class Video
{
public Thumbs thumbs { get; set; }
}
public class Thumbs
{
public string _640 { get; set; }
}
答案 1 :(得分:0)
因此,我获得了理想的结果,
string json = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlJSONcall);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
if ((responseStream != null) && responseStream.CanRead)
{
using (var reader = new System.IO.StreamReader(responseStream))
{
json = reader.ReadToEnd();
}
}
}
finally
{
if (response != null)
{
response.Close();
}
}
var datao = (JObject)JsonConvert.DeserializeObject(json);
//LBresponse.Text = data.ToString();
string urll = (string)datao["request"]["files"]["progressive"][0]["url"];
string thumbnailImage = (string)datao["video"]["thumbs"]["640"];
LBresponse.Text = urll.ToString();
lbltumb.Text = thumbnailImage.ToString();
}