C# Json反序列化失败

C# Json反序列化失败,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我正在尝试将JSON字符串反序列化为对象 引发的异常是: 无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“APIServer.Models.UserProfileModel”类型 这是JSON字符串: "id": "b483c490-8d5a-4247-b3d3-8eb7cc4208bd", "firstName": "Jeremy", "lastName": "Krasin", "gender": null, "birthDate": null, "ho

我正在尝试将JSON字符串反序列化为对象

引发的异常是:

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“APIServer.Models.UserProfileModel”类型

这是JSON字符串:

"id": "b483c490-8d5a-4247-b3d3-8eb7cc4208bd",
"firstName": "Jeremy",
"lastName": "Krasin",
"gender": null,
"birthDate": null,
"homeCountry": null,
"publicName": null,
"self": {
    "href": "http://localhost:54253/api/userprofiles/b483c490-8d5a-4247-b3d3-8eb7cc4208bd",
    "relations": [
        "collections"
    ],
    "method": "GET",
    "routeName": null,
    "routeValues": null
},
"href": "http://localhost:54253/api/userprofiles/b483c490-8d5a-4247-b3d3-8eb7cc4208bd",
"relations": [
    "collections"
],
"method": "GET",
"routeName": null,
"routeValues": null
这是我试图反序列化到的类:

public class UserProfileModel : BaseModel<UserProfileModel> {

    public Guid Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Gender { get; set; }

    public string BirthDate { get; set; }

    public string HomeCountry { get; set; }

    public string PublicName { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string PostalCode { get; set; }
}
我验证了
T
是以下类型:

APIServer.Models.UserProfileModel

我做错了什么?

调用
反序列化对象时需要指定类型,如下所示:

return JsonConvert.DeserializeObject<T>(RestClient.Execute(aRequest).Content);
                                    ^^^
返回JsonConvert.DeserializeObject(RestClient.Execute(aRequest.Content));
^^^

当您不指定类型时,
反序列化对象
将返回一个
作业对象
,该作业对象不能强制转换到您的
APIServer.Models.UserProfileModel
。这就是出现错误的原因。

调用
反序列化对象时,需要指定类型,如下所示:

return JsonConvert.DeserializeObject<T>(RestClient.Execute(aRequest).Content);
                                    ^^^
返回JsonConvert.DeserializeObject(RestClient.Execute(aRequest.Content));
^^^
当您不指定类型时,
反序列化对象
将返回一个
作业对象
,该作业对象不能强制转换到您的
APIServer.Models.UserProfileModel
。这就是为什么会出现错误