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

标签 c# asp.net json

我在 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/

相关文章:

javascript - 删除和退格键在 radeditor 中不起作用

json - *ngfor json 数组对象中的错误显示

javascript - 通过指定替换函数使用 JSON.stringify() 函数

c# - 使用 C# 为 LYNC 2010 启用用户

javascript - 如何创建函数以在后端验证下拉列表

c# - 在 silverlight 中单击子窗口外部即可关闭子窗口

c# - 如何在Unity3D Input Field UI组件中使用 "On Value Change"

asp.net - ASP.NET 如何在 </body> 标签前添加脚本代码

c# - ASP.NET MVC - 从 MemoryStream 下载 Excel 文件(损坏的文件)

ios - 解析YouTube JSON以获取UITableView