gpt4 book ai didi

c# - 为什么 JsonConvert.DeserializeObject 不起作用?

转载 作者:行者123 更新时间:2023-12-03 22:56:06 27 4
gpt4 key购买 nike

我有一个来自网络服务的结果,字符串结果是:

{"status":"success","data":{"address":"aa@aa.aa","unconfirmed":[{"tx":"cb2f252078d933f63d9cef52bee8857427d70c1142f41f10567cfad7ef1d2dcb","time_utc":"2015-03-31T19:05:09Z","amount":0.1,"n":0},{"tx":"a34fc5b8b3c29c7046ca8acaedd39280f81597a853f30825856e2f46e498c478","time_utc":"2015-03-31T19:05:01Z","amount":0.1,"n":0}]},"code":200,"message":""}

我正在调用 JsonConvert.DeserializeObject:

UTByAddressessResponse 数据 = Serializer.Deserialize(result);
public static T Deserialize<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}

public class UTByAddressessResponse
{
public string status { get; set; }
public UnconfirmedAddressInfo[] data { get; set; }
public string code { get; set; }
public string message { get; set; }
}

public class UnconfirmedAddressInfo
{
public string address { get; set; }
public List<UT> unconfirmed { get; set; }
}

public class UT
{
public string tx { get; set; }
public DateTime time_utc { get; set; }
public float amount { get; set; }
public long n { get; set; }
}

但不断收到错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Dice.Common.Objects.UnconfirmedAddressInfo[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.



为什么 ?

最佳答案

将您的 json 粘贴到 http://json2csharp.com/ 它将创建以下类

public class Unconfirmed
{
public string tx { get; set; }
public string time_utc { get; set; }
public double amount { get; set; }
public int n { get; set; }
}

public class Data
{
public string address { get; set; }
public List<Unconfirmed> unconfirmed { get; set; }
}

public class RootObject
{
public string status { get; set; }
public Data data { get; set; }
public int code { get; set; }
public string message { get; set; }
}

现在你可以反序列化为
var obj = JsonConvert.DeserializeObject<RootObject>(json_string);

PS:你可以重命名你的类名,但是如果你想重命名属性,你应该使用 JsonProperty 属性。

关于c# - 为什么 JsonConvert.DeserializeObject 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29376994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com