如何对C#对象的单个属性响应进行反序列化?
我正在测试API,目前正在测试一个返回长数据类型Id的Post方法。 我创建了一个C#对象类,其中还包含它将从另一个API调用返回的其他属性。现在,这个Post调用只返回一个Id,我想将它映射到我的类中的Id属性,但是我得到一个异常,无法从如何对C#对象的单个属性响应进行反序列化?,c#,api,deserialization,C#,Api,Deserialization,我正在测试API,目前正在测试一个返回长数据类型Id的Post方法。 我创建了一个C#对象类,其中还包含它将从另一个API调用返回的其他属性。现在,这个Post调用只返回一个Id,我想将它映射到我的类中的Id属性,但是我得到一个异常,无法从System.Int64转换到Model类 public class Model { public long id { get; set; } public string name { get; set; } public long type {
System.Int64
转换到Model类
public class Model
{
public long id { get; set; }
public string name { get; set; }
public long type { get; set; }
}
// Here is the call I am making.. FYI I am using RestSharp
response = HttpPost("URl");
var id = JsonConvert.DeserializeObject<Model.id>(restResponse.Content); //How can I map just the Id.
//这是我的模型课
public class Model
{
public long id { get; set; }
public string name { get; set; }
public long type { get; set; }
}
// Here is the call I am making.. FYI I am using RestSharp
response = HttpPost("URl");
var id = JsonConvert.DeserializeObject<Model.id>(restResponse.Content); //How can I map just the Id.
公共类模型
{
公共长id{get;set;}
公共字符串名称{get;set;}
公共长类型{get;set;}
}
//这是我正在打的电话。。仅供参考,我正在使用RestSharp
响应=HttpPost(“URl”);
var id=JsonConvert.DeserializeObject(restResponse.Content)//我怎样才能只映射Id呢。
我对API的响应是一个长数据类型,例如:658替换
Model.id
bylong
JsonConvert.DeserializeObject<long>
JsonConvert.DeserializeObject
你可以这样设置
Model model = new Model();
model.id = JsonConvert.DeserializeObject<long>...
Model Model=新模型();
model.id=JsonConvert.DeserializeObject。。。
您可以根据您的模型调整此示例
using (var client = new HttpClient())
{
string url = string.Format("your-api-url");
var response = client.GetAsync(url).Result;
string responseAsString = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<YourModel>(responseAsString);
}
public class YourModel
{
[JsonProperty("confirmed")]
public ValueModel Confirmed { get; set; }
[JsonProperty("recovered")]
public ValueModel Recovered { get; set; }
[JsonProperty("values")]
public ValueModel Values { get; set; }
}
public class ValueModel
{
[JsonProperty("value")]
public int Value { get; set; }
}
使用(var-client=new-HttpClient())
{
stringurl=string.Format(“您的api url”);
var response=client.GetAsync(url).Result;
string responseAsString=await response.Content.ReadAsStringAsync();
结果=JsonConvert.DeserializeObject(responseAsString);
}
公共类模型
{
[JsonProperty(“已确认”)]
已确认的公共价值模型{get;set;}
[JsonProperty(“已恢复”)]
已恢复的公共价值模型{get;set;}
[JsonProperty(“值”)]
公共价值模型值{get;set;}
}
公共阶级价值模型
{
[JsonProperty(“价值”)]
公共int值{get;set;}
}
将Model.id替换为longook,但如何设置它或将其映射到c#属性id