RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-320951

Konstantin Nikishonkov's questions

Martin Hope
Konstantin Nikishonkov
Asked: 2021-01-01 03:03:05 +0000 UTC

你能告诉我如何为 JSON 反序列化创建一个类吗?[复制]

  • 1
这个问题已经在这里得到了回答:
如何将 json 解析为关联数组/C# 3 个答案
3年前关闭。
{
"success": true,
"items": {
    "\u2605 Bayonet": {
        "name": "\u2605 Bayonet",
        "safe_price": "118.31",
        "safe_net_price": "110.60",
        "ongoing_price_manipulation": false,
        "total_volume": 3148,
        "7_days": {
            "median_price": "118.31",
            "median_net_price": "110.60",
            "average_price": "118.25",
            "average_net_price": "102.84",
            "lowest_price": "100.00",
            "lowest_net_price": "86.97",
            "highest_price": "132.25",
            "highest_net_price": "115.00",
            "mean_absolute_deviation": "5.46",
            "deviation_percentage": 0.046173361522199,
            "trend": -0.050389739620763,
            "volume": 94
        },
        "30_days": {
            "median_price": "138.94",
            "median_net_price": "129.88",
            "average_price": "135.69",
            "average_net_price": "118.00",
            "lowest_price": "56.03",
            "lowest_net_price": "48.73",
            "highest_price": "156.60",
            "highest_net_price": "136.19",
            "mean_absolute_deviation": "10.81",
            "deviation_percentage": 0.079666887758862,
            "trend": -0.23473444256315,
            "volume": 422
        },
        "all_time": {
            "median_price": "148.00",
            "median_net_price": "138.35",
            "average_price": "147.33",
            "average_net_price": "128.12",
            "lowest_price": "0.17",
            "lowest_net_price": "0.15",
            "highest_price": "179.24",
            "highest_net_price": "155.87",
            "mean_absolute_deviation": "9.02",
            "deviation_percentage": 0.061223104595127,
            "trend": -0.032006931201406,
            "volume": 3148
        },
        "first_seen": 1444261666
    },
    // etc ...
    "\u2605 Bayonet | Bright Water (Battle-Scarred)": {
        "name": "\u2605 Bayonet | Bright Water (Battle-Scarred)",
        "safe_price": "81.21",
        "safe_net_price": "70.62",
        "ongoing_price_manipulation": false,
        "total_volume": 1,
        "7_days": {
            "volume": 0
        },
        "30_days": {
            "median_price": "81.21",
            "median_net_price": "70.62",
            "average_price": "81.21",
            "average_net_price": "70.62",
            "lowest_price": "81.21",
            "lowest_net_price": "70.62",
            "highest_price": "81.21",
            "highest_net_price": "70.62",
            "mean_absolute_deviation": "0.00",
            "deviation_percentage": 0,
            "trend": 0,
            "volume": 1
        },
        "all_time": {
            "median_price": "81.21",
            "median_net_price": "70.62",
            "average_price": "81.21",
            "average_net_price": "70.62",
            "lowest_price": "81.21",
            "lowest_net_price": "70.62",
            "highest_price": "81.21",
            "highest_net_price": "70.62",
            "mean_absolute_deviation": "0.00",
            "deviation_percentage": 0,
            "trend": 0,
            "volume": 1
        },
        "first_seen": 1466590845
    },
    // etc ...
    "XM1014 | VariCamo Blue (Well-Worn)": {
        "name": "XM1014 | VariCamo Blue (Well-Worn)",
        "safe_price": "0.89",
        "safe_net_price": "0.84",
        "ongoing_price_manipulation": false,
        "total_volume": 763,
        "7_days": {
            "median_price": "0.89",
            "median_net_price": "0.84",
            "average_price": "0.90",
            "average_net_price": "0.80",
            "lowest_price": "0.65",
            "lowest_net_price": "0.58",
            "highest_price": "1.08",
            "highest_net_price": "0.95",
            "mean_absolute_deviation": "0.09",
            "deviation_percentage": 0.1,
            "trend": -0.10752688172043,
            "volume": 30
        },
        "30_days": {
            "median_price": "1.12",
            "median_net_price": "0.99",
            "average_price": "1.10",
            "average_net_price": "0.96",
            "lowest_price": "0.65",
            "lowest_net_price": "0.58",
            "highest_price": "1.51",
            "highest_net_price": "1.32",
            "mean_absolute_deviation": "0.16",
            "deviation_percentage": 0.14545454545455,
            "trend": -0.32851070467574,
            "volume": 153
        },
        "all_time": {
            "median_price": "1.70",
            "median_net_price": "1.49",
            "average_price": "1.71",
            "average_net_price": "1.50",
            "lowest_price": "0.03",
            "lowest_net_price": "0.01",
            "highest_price": "11.50",
            "highest_net_price": "10.00",
            "mean_absolute_deviation": "0.41",
            "deviation_percentage": 0.23976608187135,
            "trend": -0.67071929583276,
            "volume": 763
        },
        "first_seen": 1444277043
    }
},
"build_time": 19954,
"updated_at": 1467408505

}

我在“项目”中每个新项目都有一个新名称的那一刻感到困惑。目前这就是我所拥有的:

   public class 7Days
{
    public string median_price { get; set; }
    public string median_net_price { get; set; }
    public string average_price { get; set; }
    public string average_net_price { get; set; }
    public string lowest_price { get; set; }
    public string lowest_net_price { get; set; }
    public string highest_price { get; set; }
    public string highest_net_price { get; set; }
    public string mean_absolute_deviation { get; set; }
    public double deviation_percentage { get; set; }
    public double trend { get; set; }
    public int volume { get; set; }
}

public class 30Days
{
    public string median_price { get; set; }
    public string median_net_price { get; set; }
    public string average_price { get; set; }
    public string average_net_price { get; set; }
    public string lowest_price { get; set; }
    public string lowest_net_price { get; set; }
    public string highest_price { get; set; }
    public string highest_net_price { get; set; }
    public string mean_absolute_deviation { get; set; }
    public double deviation_percentage { get; set; }
    public double trend { get; set; }
    public int volume { get; set; }
}

public class AllTime
{
    public string median_price { get; set; }
    public string median_net_price { get; set; }
    public string average_price { get; set; }
    public string average_net_price { get; set; }
    public string lowest_price { get; set; }
    public string lowest_net_price { get; set; }
    public string highest_price { get; set; }
    public string highest_net_price { get; set; }
    public string mean_absolute_deviation { get; set; }
    public double deviation_percentage { get; set; }
    public double trend { get; set; }
    public int volume { get; set; }
}

public class Bayonet
{
    public string name { get; set; }
    public string safe_price { get; set; }
    public string safe_net_price { get; set; }
    public bool ongoing_price_manipulation { get; set; }
    public int total_volume { get; set; }
    public 7Days 7_days { get; set; }
    public 30Days 30_days { get; set; }
    public AllTime all_time { get; set; }
    public int first_seen { get; set; }
}

public class BayonetBrightWaterBattleScarred
{
    public string name { get; set; }
    public string safe_price { get; set; }
    public string safe_net_price { get; set; }
    public bool ongoing_price_manipulation { get; set; }
    public int total_volume { get; set; }
    public  7_days { get; set; }
    public  30_days { get; set; }
    public  all_time { get; set; }
    public int first_seen { get; set; }
}

public class XM1014VariCamoBlueWellWorn
{
    public string name { get; set; }
    public string safe_price { get; set; }
    public string safe_net_price { get; set; }
    public bool ongoing_price_manipulation { get; set; }
    public int total_volume { get; set; }
    public  7_days { get; set; }
    public  30_days { get; set; }
    public  all_time { get; set; }
    public int first_seen { get; set; }
}

public class Items
{
    public Bayonet ★ Bayonet { get; set; }
    public BayonetBrightWaterBattleScarred ★ Bayonet | Bright Water (Battle-Scarred) { get; set; }
    public XM1014VariCamoBlueWellWorn XM1014 | VariCamo Blue (Well-Worn) { get; set; }
}

public class Example
{
    public bool success { get; set; }
    public Items items { get; set; }
    public int build_time { get; set; }
    public int updated_at { get; set; }
}

但是我需要在有很多不同名称的项目的地方反序列化 json,告诉我如何避免使用每个项目名称创建类?祝大家新年快乐)))

UPD。我重做了课程,这就是发生的事情:

{
public class _7Days
{
public string median_price { get; set; }
public string median_net_price { get; set; }
public string average_price { get; set; }
public string average_net_price { get; set; }
public string lowest_price { get; set; }
public string lowest_net_price { get; set; }
public string highest_price { get; set; }
public string highest_net_price { get; set; }
public string mean_absolute_deviation { get; set; }
public double deviation_percentage { get; set; }
public double trend { get; set; }
public int volume { get; set; }
}

public class _30Days
{
public string median_price { get; set; }
public string median_net_price { get; set; }
public string average_price { get; set; }
public string average_net_price { get; set; }
public string lowest_price { get; set; }
public string lowest_net_price { get; set; }
public string highest_price { get; set; }
public string highest_net_price { get; set; }
public string mean_absolute_deviation { get; set; }
public double deviation_percentage { get; set; }
public double trend { get; set; }
public int volume { get; set; }
}

public class _AllTime
{
public string median_price { get; set; }
public string median_net_price { get; set; }
public string average_price { get; set; }
public string average_net_price { get; set; }
public string lowest_price { get; set; }
public string lowest_net_price { get; set; }
public string highest_price { get; set; }
public string highest_net_price { get; set; }
public string mean_absolute_deviation { get; set; }
public double deviation_percentage { get; set; }
public double trend { get; set; }
public int volume { get; set; }
}

public class Item
{
public string name { get; set; }
public string safe_price { get; set; }
public string safe_net_price { get; set; }
public bool ongoing_price_manipulation { get; set; }
public int total_volume { get; set; }
public _AllTime all_time { get; set; }
public _7Days _7_days { get; set; }
public _30Days _30_days { get; set; }


public int first_seen { get; set; }
}

public class Rootobject
{
public bool success { get; set; }
public Dictionary<string, Item> item { get; set; }
public int build_time { get; set; }
public int updated_at { get; set; }
}
}

创建了一个读取 json 的附加类:

public static Rootobject GetRoot()
    {
        string path = @"C:\Users\DB.json";
        StreamReader sr = new StreamReader(path);
        string itemDB = sr.ReadToEnd();
        return JsonConvert.DeserializeObject<Rootobject>(itemDB);                

    }

在我的程序中,我调用:

Rootobject itemsDB = PricerApi.GetRoot();

但是在 itemsDB 的 item=null 字段中,虽然从文件中读取并执行反序列化时,所有项目都存在于 item 字段中,我打开并查看它们,一切都与 json 中应有的完全一样,我做了什么错误的?

c#
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5