C# 字符串对象无法反序列化为特定对象C 以字符串格式序列化数据 对象类 对反序列化对象使用Newtonsoft.Json程序集 尝试1:

C# 字符串对象无法反序列化为特定对象C 以字符串格式序列化数据 对象类 对反序列化对象使用Newtonsoft.Json程序集 尝试1:,c#,.net,json,json.net,deserialization,C#,.net,Json,Json.net,Deserialization,输出 我无法反序列化对象。如何将其转换为我的对象类?你不妨试试serializer.Deserializestr;例如,var result=jsoninvert.DeserializeObjectjsonString 您的JSON包含VerifyOTPResult,但实际上是一个具有VerifyOTPResult属性的不同对象。您应该反序列化到某个包装类,即VerifyOTPResultResponse string data = "{\"VerifyOTPResult\":{\"Return

输出

我无法反序列化对象。如何将其转换为我的对象类?

你不妨试试serializer.Deserializestr;例如,var result=jsoninvert.DeserializeObjectjsonString

您的JSON包含VerifyOTPResult,但实际上是一个具有VerifyOTPResult属性的不同对象。您应该反序列化到某个包装类,即VerifyOTPResultResponse

string data = "{\"VerifyOTPResult\":{\"ReturnCode\":\"200\",\"ReturnMsg\":\"Invalid OTP.\",\"Data\":{\"BrokerName\":null,\"ErrorMsg\":null,\"Id\":null,\"IsValidUser\":false,\"RoleName\":null}}}";
public class VerifyOTPResult {
  public string ReturnCode { get; set; }
  public string ReturnMsg { get; set; }
  public ValidateUserResult Data { get; set; }
}

public class ValidateUserResult {
  public string Id { get; set; }
  public bool IsValidUser { get; set; }
  public string BrokerName { get; set; }        
  public string RoleName { get; set; }
  public string ErrorMsg { get; set; }
}
var decRes = JsonConvert.DeserializeObject<VerifyOTPResult>(content);
var decRes1 = JsonConvert.DeserializeObject(content);
{
    "VerifyOTPResult": {
        "ReturnCode": "200",
        "ReturnMsg": "Invalid OTP.",
        "Status": null,
        "CurrentPage": 0,
        "Data": {
            "BrokerName": null,
            "ErrorMsg": null,
            "Id": null,
            "IsValidUser": false,
            "RoleName": null
        }
    }
}
void Main()
{
    string data = "{\"VerifyOTPResult\":{\"ReturnCode\":\"200\",\"ReturnMsg\":\"Invalid OTP.\",\"Data\":{\"BrokerName\":null,\"ErrorMsg\":null,\"Id\":null,\"IsValidUser\":false,\"RoleName\":null}}}";

    var decRes = JsonConvert.DeserializeObject<VerifyOTPResultResponse>(data);

    Console.WriteLine(decRes.VerifyOTPResult.ReturnCode);
    Console.WriteLine(decRes.VerifyOTPResult.ReturnMsg);

    // Output:
    // 200
    // Invalid OTP.
}

public class VerifyOTPResultResponse
{
    public VerifyOTPResult VerifyOTPResult { get; set; }
}

public class VerifyOTPResult
{
    public string ReturnCode { get; set; }
    public string ReturnMsg { get; set; }
    public ValidateUserResult Data { get; set; }
}

public class ValidateUserResult
{
    public string Id { get; set; }
    public bool IsValidUser { get; set; }
    public string BrokerName { get; set; }
    public string RoleName { get; set; }
    public string ErrorMsg { get; set; }
}