简体   繁体   English

在对象中存储JSON REST响应

[英]Storing JSON REST response in object

I am new to C# and I am trying to work out how to store my HTTPResponseMessage in an object. 我是C#新手,我正在尝试找出如何将HTTPResponseMessage存储在对象中。

static async Task RunAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://api.themoviedb.org/3/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("search/tv?api_key=MY_KEYa&query=hawaii%20five%20o&first_air_date_year=2010");                

            if (response.IsSuccessStatusCode)
            {


                // What to do here..?


            }
        }
    }

I used this site ( http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client ) for the above code. 我将这个网站( http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client )用于上面的代码。

Using this website ( http://json2csharp.com/ ), I created the following class: 使用此网站( http://json2csharp.com/ ),我创建了以下类:

class TVShowDTO
{
    public string backdrop_path { get; set; }
    public int id { get; set; }
    public string original_name { get; set; }
    public string first_air_date { get; set; }
    public List<string> origin_country { get; set; }
    public string poster_path { get; set; }
    public double popularity { get; set; }
    public string name { get; set; }
    public int vote_average { get; set; }
    public int vote_count { get; set; }
}

API Response: API响应:

{
   page: 1
   results: [1]
   0:  {
   backdrop_path: "/mAXpHFDTMHvJt7WdibFWdbRsdCG.jpg"
   id: 32798
   original_name: "Hawaii Five-0"
   first_air_date: "2010-09-20"
   origin_country: [1]
   0:  "US"
   -
   poster_path: "/hO4BgEJhGIrFJ7f00sR6ZcdNB6y.jpg"
   popularity: 1.06099324148627
   name: "Hawaii Five-0"
   vote_average: 7.3
   vote_count: 6
   }-
   -
   total_pages: 1
   total_results: 1
}

Here is a link to the API I am trying to access: http://docs.themoviedb.apiary.io/reference/search/searchtv/get 这是我尝试访问的API的链接: http : //docs.themoviedb.apiary.io/reference/search/searchtv/get

Do I need to get the response as a string first? 我需要首先获取response作为string吗? Because the examples I have seen of this on SO, seem to use an input string . 因为我在SO上看到的示例似乎使用了输入string

I haven't been able to work this out as when I print response.Content , it just returns "System.Net.Http.StreamContent" 我无法解决此问题,因为当我打印response.Content ,它只返回"System.Net.Http.StreamContent"

Any guidance with this would be greatly appreciated. 任何与此有关的指导将不胜感激。

First, you'll want to get a string of the response: 首先,您需要获取响应字符串:

string responseContent = await response.Content.ReadAsStringAsync();

Then, you can use a tool like Json.NET from NUGET to deserialize to your object: 然后,您可以使用NUGET中的Json.NET之类的工具反序列化您的对象:

var obj = JsonConvert.DeserializeObject<TVShowDTO>(responseContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM