.NET通过GET API从C#的标头反序列化JSON [英] .NET Deserialize JSON from GET API with header from C#

查看:57
本文介绍了.NET通过GET API从C#的标头反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几个小时的研究,我找不到转换这种JSON的方法:

After a couple hours of research I can't find a way to transform that kind of JSON :

https://api.jamendo.com/v3.0/tracks/?client_id=56d30c95&format=jsonpretty&id=982090

{
    "headers":{
        "status":"success",
        "code":0,
        "error_message":"",
        "warnings":"",
        "results_count":1
    },
    "results":[
        {
            "id":"982090",
            "name":"Seul",
            "duration":297,
            "artist_id":"350774",
            "artist_name":"DON VALDES",
            "artist_idstr":"DON_VALDES",
            "album_name":"EVOLUTION",
            "album_id":"115666",
            "license_ccurl":"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/",
            "position":10,
            "releasedate":"2012-11-23",
            "album_image":"https:\/\/imgjam2.jamendo.com\/albums\/s115\/115666\/covers\/1.200.jpg",
            "audio":"https:\/\/mp3l.jamendo.com\/?trackid=982090&format=mp31&from=app-56d30c95",
            "audiodownload":"https:\/\/mp3d.jamendo.com\/download\/track\/982090\/mp32\/",
            "prourl":"https:\/\/licensing.jamendo.com\/track\/982090",
            "shorturl":"http:\/\/jamen.do\/t\/982090",
            "shareurl":"http:\/\/www.jamendo.com\/track\/982090",
            "image":"https:\/\/imgjam2.jamendo.com\/albums\/s115\/115666\/covers\/1.200.jpg"
        }
    ]
}

这是我的方法,但是我的对象"track"中的每个值都为null,我认为这是因为它无法到达JSON的"results"部分(并且我找不到如何关注此部分)或这是因为它无法到达结果"的第一个成员([0])(而且我也无法专注于第一个元素:/):

Here is my method but every values in my object "track" is null and I think it's because it can't reach the "results" part of the JSON (and I can't find how to focus this part) or it's because it can't reach the first member ([0]) of "result" (and I can't a way to focus on that first element either :/) :

public static TrackModel getTrackById(int id)
    {
        Uri uri = new Uri(String.Format("https://api.jamendo.com/v3.0/tracks/?client_id=56d30c95&format=jsonpretty&id={0}", id));
        WebRequest webRequest = WebRequest.Create(uri);
        WebResponse response = webRequest.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        String responseData = streamReader.ReadToEnd();

        var track = JsonConvert.DeserializeObject<TrackModel>(responseData).;

        return track;
    }

希望您能为我提供帮助,

I hope you will be able to help me,

非常感谢您!

推荐答案

TrackModel 分解为两个属性,并为 Result 提供一个单独的类.

Break down your TrackModel into two properties and have a separate class for the Result.

public class TrackModel
{
    [JsonProperty(PropertyName = "headers")]
    public Headers Headers { get; set; }
    [JsonProperty(PropertyName = "results")]
    public Result[] Results { get; set; }
}

public class Headers
{
    [JsonProperty(PropertyName = "status")]
    public string Status { get; set; }
    [JsonProperty(PropertyName = "code")]
    public string Code { get; set; }
    [JsonProperty(PropertyName = "error_message")]
    public string Errormessage { get; set; }
    [JsonProperty(PropertyName = "warnings")]
    public string Warnings { get; set; }
    [JsonProperty(PropertyName = "results_count")]
    public string ResultsCount { get; set; }
}

public class Result
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Duration { get; set; }
    [JsonProperty(PropertyName = "artist_id")]
    public string ArtistId { get; set; }
    [JsonProperty(PropertyName = "artist_name")]
    public string ArtistName { get; set; }
    [JsonProperty(PropertyName = "artist_idstr")]
    public string ArtistIdstr { get; set; }
    [JsonProperty(PropertyName = "album_name")]
    public string AlbumName { get; set; }
    [JsonProperty(PropertyName = "album_id")]
    public string AlbumId { get; set; }
    [JsonProperty(PropertyName = "license_ccurl")]
    public string LicenseCcurl { get; set; }
    public int Position { get; set; }
    public string Releasedate { get; set; }
    [JsonProperty(PropertyName = "album_image")]
    public string AlbumImage { get; set; }
    public string Audio { get; set; }
    public string Audiodownload { get; set; }
    public string Prourl { get; set; }
    public string Shorturl { get; set; }
    public string Shareurl { get; set; }
    public string Image { get; set; }
}

在您的原始方法中使用

var track = JsonConvert.DeserializeObject< TrackModel>(responseData).results [0] .album_id;

这篇关于.NET通过GET API从C#的标头反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆