使用 RestSharp 将 JSON 反序列化为对象或数组
Deserialize JSON to an object or an array using RestSharp
我正在处理 Xamarin 项目。基本上,我想从 API 接收数据并显示它。为此,我正在使用 RestSharp。
这是我的 API 请求代码。
string test;
test = "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq";
string s = string.Format("http://192.168.1.4:3116/api/user/getuser/{0}", test);
client = new RestClient(s);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
IRestResponse response2 = client.Execute(request);
这是我收到的 JSON 对象。
{
"id": 1,
"token": "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq",
"email": "some email",
"password": "testpassword",
"currentCompany": "some company",
"currentRole": "Software Developer",
"date": "Something",
"name": "Some name",
"lastName": "Some surname",
"headLine": "Some text",
"education": "University of Hotshots",
"country": "Who cares",
"imageLocation": "Some url"
}
这是我使用网站为它创建的class:json2csharp.com/
class User
{
public int Id { get; set; }
public string Token { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string CurrentCompany { get; set; }
public string CurrentRole { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string HeadLine { get; set; }
public string Education { get; set; }
public string Country { get; set; }
public string ImageLocation { get; set; }
}
如何更改我的代码,以便反序列化对上述 class 实例的响应,以便我可以使用它来处理数据?我阅读了此处的帖子并尝试了解决方案,但它们似乎对我不起作用。所以,我发布了这个问题。
使用数组也是一种选择;我可以使用它。作为参考,请参阅 PHP 的 $array = json_decode($somepostrequest, true)
,它将 JSON 对象转换为关联数组。你可以只调用对象的$array['email']
。
使用Newtonsoft.JSON将JSON反序列化为一个对象(反之亦然)。它非常简单并且可以免费使用。还有一个 NugetPackage 可用。
安装后,您只需要以下行即可获取所需的对象。
User userObj = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(jsonString);
尝试使用 client.Execute()
的通用重载。 RestSharp 将使用其内部序列化器反序列化 JSON:
var response2 = client.Execute<User>(request);
User user = response2.Data;
RestSharp GitHub 站点上的 Recommended Usage wiki 页面显示了一个示例。
诚然,RestSharp 的内部序列化程序不如 Json.Net 功能齐全,但看起来您不需要任何花哨的东西。
我正在处理 Xamarin 项目。基本上,我想从 API 接收数据并显示它。为此,我正在使用 RestSharp。
这是我的 API 请求代码。
string test;
test = "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq";
string s = string.Format("http://192.168.1.4:3116/api/user/getuser/{0}", test);
client = new RestClient(s);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
IRestResponse response2 = client.Execute(request);
这是我收到的 JSON 对象。
{
"id": 1,
"token": "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq",
"email": "some email",
"password": "testpassword",
"currentCompany": "some company",
"currentRole": "Software Developer",
"date": "Something",
"name": "Some name",
"lastName": "Some surname",
"headLine": "Some text",
"education": "University of Hotshots",
"country": "Who cares",
"imageLocation": "Some url"
}
这是我使用网站为它创建的class:json2csharp.com/
class User
{
public int Id { get; set; }
public string Token { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string CurrentCompany { get; set; }
public string CurrentRole { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string HeadLine { get; set; }
public string Education { get; set; }
public string Country { get; set; }
public string ImageLocation { get; set; }
}
如何更改我的代码,以便反序列化对上述 class 实例的响应,以便我可以使用它来处理数据?我阅读了此处的帖子并尝试了解决方案,但它们似乎对我不起作用。所以,我发布了这个问题。
使用数组也是一种选择;我可以使用它。作为参考,请参阅 PHP 的 $array = json_decode($somepostrequest, true)
,它将 JSON 对象转换为关联数组。你可以只调用对象的$array['email']
。
使用Newtonsoft.JSON将JSON反序列化为一个对象(反之亦然)。它非常简单并且可以免费使用。还有一个 NugetPackage 可用。
安装后,您只需要以下行即可获取所需的对象。
User userObj = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(jsonString);
尝试使用 client.Execute()
的通用重载。 RestSharp 将使用其内部序列化器反序列化 JSON:
var response2 = client.Execute<User>(request);
User user = response2.Data;
RestSharp GitHub 站点上的 Recommended Usage wiki 页面显示了一个示例。
诚然,RestSharp 的内部序列化程序不如 Json.Net 功能齐全,但看起来您不需要任何花哨的东西。