首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >部分反序列化JSON以仅获取所需的属性

部分反序列化JSON以仅获取所需的属性
EN

Stack Overflow用户
提问于 2018-07-26 17:36:31
回答 2查看 99关注 0票数 2

我在JSON格式中得到了很大的响应,我只需要2个字符串,这里是我的JSON文件,我简化了它以便更好地阅读

代码语言:javascript
代码运行次数:0
运行
复制
{
  "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变量,这是在视频对象。

代码语言:javascript
代码运行次数:0
运行
复制
    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);

    }
}

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

我不知道下一步该做什么

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-30 19:26:43

我通过这个得到了想要的结果,

代码语言:javascript
代码运行次数: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();
    }
票数 0
EN

Stack Overflow用户

发布于 2018-07-26 18:11:45

这对我很有效:

代码语言:javascript
代码运行次数: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:

代码语言:javascript
代码运行次数: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 
            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;
            }
        }
    }

Step2:添加这些类以浏览属性

代码语言:javascript
代码运行次数:0
运行
复制
   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; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51535649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档