如何将 JSON return 反序列化为从 RestSharp 调用到 API 的对象数组?
How to deserialize a JSON return into an array of objects from a RestSharp call to an API?
我正在尝试将 JSON return 从 RestSharp 调用反序列化为 API。
这是一个 C# 控制台应用程序。到目前为止,这是我的代码:
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);
}
}
}
响应是 JSON 格式的消息列表,键包括 "to"
、"from"
、"body"
和 "status"
。这是它的样子:
{
"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"
},
我想把它变成一个 Message
对象数组,Message
class 看起来像这样:
class Message
{
public string To { get; set; }
public string From { get; set; }
public string Body { get; set; }
public string Status { get; set; }
}
我一直在寻找如何将 JSON 变成对象并找到了这个页面:http://blog.atavisticsoftware.com/2014/02/using-restsharp-to-deserialize-json.html。页面上的第三个示例符合我的要求。
我对程序做了一些修改,现在它看起来像这样:
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();
}
}
但是,当我运行程序时,messageList
是空的。将 JSON 响应转换为对象列表我缺少什么? RestSharp Documentation 使它看起来相当简单。
我不太了解RestSharp,但是你可以得到这样的响应内容字符串:
var responseContent = client.Execute(request).Content;
并使用 Json.Net 反序列化您的 json 字符串:
JObject jsonResponse = (JObject)JsonConvert.DeserializeObject(response.Content);
var messageList = JsonConvert.DeserializeObject<List<Message>>(jsonResponse["messages"].ToString());
我正在尝试将 JSON return 从 RestSharp 调用反序列化为 API。
这是一个 C# 控制台应用程序。到目前为止,这是我的代码:
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);
}
}
}
响应是 JSON 格式的消息列表,键包括 "to"
、"from"
、"body"
和 "status"
。这是它的样子:
{
"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"
},
我想把它变成一个 Message
对象数组,Message
class 看起来像这样:
class Message
{
public string To { get; set; }
public string From { get; set; }
public string Body { get; set; }
public string Status { get; set; }
}
我一直在寻找如何将 JSON 变成对象并找到了这个页面:http://blog.atavisticsoftware.com/2014/02/using-restsharp-to-deserialize-json.html。页面上的第三个示例符合我的要求。
我对程序做了一些修改,现在它看起来像这样:
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();
}
}
但是,当我运行程序时,messageList
是空的。将 JSON 响应转换为对象列表我缺少什么? RestSharp Documentation 使它看起来相当简单。
我不太了解RestSharp,但是你可以得到这样的响应内容字符串:
var responseContent = client.Execute(request).Content;
并使用 Json.Net 反序列化您的 json 字符串:
JObject jsonResponse = (JObject)JsonConvert.DeserializeObject(response.Content);
var messageList = JsonConvert.DeserializeObject<List<Message>>(jsonResponse["messages"].ToString());