gpt4 book ai didi

c# - 部分反序列化 JSON 以仅获取必需的属性

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:36 25 4
gpt4 key购买 nike

我在 JSON formate 中得到了很大的响应,我只需要 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);

}
}

由于嵌套对象,我很难解决它。

我不知道下一步该做什么,

最佳答案

这对我有用:

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

第 2 步:添加这些类以浏览属性

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

关于c# - 部分反序列化 JSON 以仅获取必需的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com