我在做一个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"
}
这是我使用网站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; }
}
如何更改代码,以便对上述类的实例的响应进行反序列化,以便使用它来处理数据?我阅读了这里的帖子并尝试了解决方案,但它们似乎对我不起作用。所以,我发布了这个问题。
使用数组也是一种选择;我可以用它。参考请参见PHP
$array = json_decode($somepostrequest, true)
,它将JSON对象转换为关联数组。您可以调用对象的
$array['email']
.