gpt4 book ai didi

c# - 尝试反序列化 JSON 时获取 NullReferenceException

转载 作者:行者123 更新时间:2023-11-30 16:43:21 25 4
gpt4 key购买 nike

所以我正在努力思考如何正确地反序列化它。

{
"_note": "You are currently authenticated with the API using your OPSkins login session cookies.",
"status": 1,
"time": 1500460072,
"response": {
"AK-47 | Aquamarine Revenge (Battle-Scarred)": {
"op_7_day": 999,
"op_30_day": 932
},
"AK-47 | Aquamarine Revenge (Factory New)": {
"op_7_day": 2738,
"op_30_day": 2665
}
}
}

这是我的类结构

public class OpRoot
{
public string _note { get; set; }
public int status { get; set; }
public int time { get; set; }
public OpResponse response { get; set; }
}

public class OpResponse
{
public Dictionary<string, OpItem> items { get; set; }
}

public class OpItem
{
public int op_7_day { get; set; }
public int op_30_day { get; set; }
}

这就是我尝试反序列化它的方式:

OpRoot OpInstance = JsonConvert.DeserializeObject<OpRoot>(readerResponse);

我曾尝试将 Dictionary 改为 List,但在尝试调用“items”对象时得到了相同的结果“System.NullReferenceException”:

Console.WriteLine(OpInstance.response.items.Values);

所以我认为问题出在“OpResponse”类的代码中。此代码的大部分以前都可以使用,但是使用的是另一种 JSON 结构。

任何帮助将不胜感激

编辑:修复错字

最佳答案

您不需要 OpResponse。对于以下类,它应该可以工作:

public class OpRoot
{
public string _note { get; set; }
public int status { get; set; }
public int time { get; set; }
public Dictionary<string, OpItem> response { get; set; }
}

public class OpItem
{
public int op_7_day { get; set; }
public int op_30_day { get; set; }
}

关于c# - 尝试反序列化 JSON 时获取 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190695/

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