简体   繁体   English

如何将JSON返回反序列化为从RestSharp调用到API的对象数组?

[英]How to deserialize a JSON return into an array of objects from a RestSharp call to an API?

I am trying to deserialize a JSON return from a RestSharp call to an API. 我试图将从RestSharp调用的JSON返回反序列化为API。

This is a C# console application. 这是一个C#控制台应用程序。 Here is my code so far: 到目前为止,这是我的代码:

using System;
using RestSharp;
using RestSharp.Authenticators;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RestSharp.Deserializers;
using System.Collections.Generic;

namespace TwilioTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new RestClient("https://api.twilio.com/2010-04-01");
            var request = new RestRequest("Accounts/{{Account Sid}}/Messages.json", Method.GET);
            request.AddParameter("To", "{{phone number}}");
            request.AddParameter("From", "{{phone number}}");
            client.Authenticator = new HttpBasicAuthenticator("{{account sid}}", "{{auth token}}");
            var response = client.Execute(request);
            var jsonResponse = JsonConvert.DeserializeObject(response.Content);
            Console.WriteLine(jsonResponse);
        }
    }
}

The response is a JSON-formatted list of messages, with keys including "to" , "from" , "body" , and "status" . 响应是JSON格式的消息列表,其中包括"to""from""body""status" Here's sort of what that looks like: 这是什么样的:

{
  "to": "{{phone number}}",
  "from": "{{phone number}}",
  "body": "Sent from your Twilio trial account - Message 1!",
  "status": "delivered"
},
{
  "to": "{{phone number}}",
  "from": "{{phone number}}",
  "body": "Sent from your Twilio trial account - Message 2",
  "status": "delivered"
},

I want to turn that into an array of Message objects, with a Message class that looks like this: 我想将其转换为Message对象数组,其Message类如下所示:

class Message
{
    public string To { get; set; }
    public string From { get; set; }
    public string Body { get; set; }
    public string Status { get; set; }
}

I've been looking around for how to turn JSON into objects and found this page: http://blog.atavisticsoftware.com/2014/02/using-restsharp-to-deserialize-json.html . 我一直在寻找如何将JSON转换为对象并找到这个页面: http//blog.atavisticsoftware.com/2014/02/using-restsharp-to-deserialize-json.html The third example on the page is along the lines of what I want. 页面上的第三个示例与我想要的一致。

I made some changes to the program and now it looks like this: 我对程序进行了一些更改,现在它看起来像这样:

    class Program
    {
        static void Main(string[] args)
        {
            // same as above until this point
            ...
            var response = client.Execute(request);
            JsonDeserializer deserial = new JsonDeserializer();
            var messageList = deserial.Deserialize<List<Message>>(response);
            foreach (Message element in messageList)
            {
                Console.WriteLine("To: {0}", element.To);
                Console.WriteLine("From: {0}", element.From);
                Console.WriteLine("Body: {0}", element.Body);

            }
            Console.ReadLine();
        }
    }

However, messageList is empty when I run the program. 但是,当我运行程序时, messageList为空。 What am I missing to turn the JSON response into a list of objects? 将JSON响应转换为对象列表,我错过了什么? The RestSharp Documentation makes it look fairly straightforward. RestSharp文档使它看起来相当简单。

I don't know much about RestSharp, but you can get the response content string like that : 我对RestSharp了解不多,但您可以获得响应内容字符串:

var responseContent = client.Execute(request).Content;

And use Json.Net to deserialize your json string : 并使用Json.Net反序列化您的json字符串:

JObject jsonResponse = (JObject)JsonConvert.DeserializeObject(response.Content);
var messageList = JsonConvert.DeserializeObject<List<Message>>(jsonResponse["messages"].ToString()‌​);

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

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