将 JToken 内容反序列化为对象
Deserializing JToken content to an Object
我想将 JToken 内容反序列化为对象 (User
)。我怎样才能做到这一点?
这是我的 json 字符串:
string json = @"[{""UserId"":0,""Username"":""jj.stranger"",""FirstName"":""JJ"",""LastName"":""stranger""}]";
这被发送到 api 参数作为 JToken
。
用户class:
public class user
{
public int UserId {get; set;}
public string Username {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
网络Api方法:
public IHttpActionResult Post([FromBody]JToken users)
{
UserModel.SaveUser(users);
//...
}
API 在 Salesforce 中调用:
string json = '[{"UserId":0,"Username":"jj.stranger","FirstName":"JJ","LastName":"stranger"}]';
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('test.com/api/UserManagement');
req.setMethod('POST');
req.setBody(json);
req.setHeader('Content-Type', 'application/json');
try {
res = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error:' + e);
}
System.debug(res.getBody());
您可以使用 JToken.ToObject 泛型方法。
http://www.nudoq.org/#!/Packages/Newtonsoft.Json/Newtonsoft.Json/JToken/M/ToObject(T)
服务器API代码:
public void Test(JToken users)
{
var usersArray = users.ToObject<User[]>();
}
这是我使用的客户端代码。
string json = "[{\"UserId\":0,\"Username\":\"jj.stranger\",\"FirstName\":\"JJ\",\"LastName\":\"stranger\"}]";
HttpClient client = new HttpClient();
var result = client.PostAsync(@"http://localhost:50577/api/values/test", new StringContent(json, Encoding.UTF8, "application/json")).Result;
对象毫无问题地转换为 Users 数组。
我想将 JToken 内容反序列化为对象 (User
)。我怎样才能做到这一点?
这是我的 json 字符串:
string json = @"[{""UserId"":0,""Username"":""jj.stranger"",""FirstName"":""JJ"",""LastName"":""stranger""}]";
这被发送到 api 参数作为 JToken
。
用户class:
public class user
{
public int UserId {get; set;}
public string Username {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
网络Api方法:
public IHttpActionResult Post([FromBody]JToken users)
{
UserModel.SaveUser(users);
//...
}
API 在 Salesforce 中调用:
string json = '[{"UserId":0,"Username":"jj.stranger","FirstName":"JJ","LastName":"stranger"}]';
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('test.com/api/UserManagement');
req.setMethod('POST');
req.setBody(json);
req.setHeader('Content-Type', 'application/json');
try {
res = http.send(req);
} catch(System.CalloutException e) {
System.debug('Callout error:' + e);
}
System.debug(res.getBody());
您可以使用 JToken.ToObject 泛型方法。 http://www.nudoq.org/#!/Packages/Newtonsoft.Json/Newtonsoft.Json/JToken/M/ToObject(T)
服务器API代码:
public void Test(JToken users)
{
var usersArray = users.ToObject<User[]>();
}
这是我使用的客户端代码。
string json = "[{\"UserId\":0,\"Username\":\"jj.stranger\",\"FirstName\":\"JJ\",\"LastName\":\"stranger\"}]";
HttpClient client = new HttpClient();
var result = client.PostAsync(@"http://localhost:50577/api/values/test", new StringContent(json, Encoding.UTF8, "application/json")).Result;
对象毫无问题地转换为 Users 数组。