c#解析不';不要使用JSON数组

c#解析不';不要使用JSON数组,c#,.net,json,C#,.net,Json,我有一个嵌套的json字符串,而不是使用数组,下一层是另一个json结构。这会造成使用传统方法进行反序列化的混乱 处理json解析的大多数其他答案都有明确定义的结构,并且在大多数情况下可以使用在线工具(例如)来解决。但是因为这个JSON没有正确地使用数组,所以我很难想出一个反序列化它的解决方案 例如: { "time":1516824466, "global":{ "workers":1, "hashrate":0 }, "algos":{

我有一个嵌套的json字符串,而不是使用数组,下一层是另一个json结构。这会造成使用传统方法进行反序列化的混乱

处理json解析的大多数其他答案都有明确定义的结构,并且在大多数情况下可以使用在线工具(例如)来解决。但是因为这个JSON没有正确地使用数组,所以我很难想出一个反序列化它的解决方案

例如:

 {
   "time":1516824466,
   "global":{
      "workers":1,
      "hashrate":0
   },
   "algos":{
      "scrypt-n":{
         "workers":1,
         "hashrate":79752.92436043094,
         "hashrateString":"79.75 KH"
      }
   },
   "pools":{
      "garlicoin":{
         "name":"garlicoin",
         "symbol":"GRLC",
         "algorithm":"scrypt-n",
         "poolStats":{
            "validShares":"22855",
            "validBlocks":"3",
            "invalidShares":"59",
            "invalidRate":"0.0026",
            "totalPaid":"296.42722209999999999"
         },
         "blocks":{
            "pending":0,
            "confirmed":2,
            "orphaned":1
         },
         "workers":{
            "Gf3ZXqhWKkm8qLhSHvyrawiCiooYeU9eQu":{
               "shares":365.07991498000007,
               "invalidshares":0,
               "hashrate":79752.92436043094,
               "hashrateString":"79.75 KH"
            },
            "Gz2Llan6hTkm8qLhSHh34awiCiooYe17heT":{
               "shares":365.07991498000007,
               "invalidshares":0,
               "hashrate":79752.92436043094,
               "hashrateString":"79.75 KH"
            }
         },
         "hashrate":79752.92436043094,
         "workerCount":1,
         "hashrateString":"79.75 KH"
      }
   }
}
我无法具体地反序列化这两部分:

"algos":{
  "scrypt-n":{
     "workers":1,
     "hashrate":79752.92436043094,
     "hashrateString":"79.75 KH"
  }
},

我尝试过的代码

namespace pooldecode
{

公共静态类序列化
{
公共静态字符串ToJson(此jsonDecode.Root self)
{
返回JsonConvert.SerializeObject(self,Converter.Settings);
}
}
公共类转换器
{
公共静态只读JsonSerializerSettings设置=新JsonSerializerSettings
{
MetadataPropertyHandling=MetadataPropertyHandling.Ignore,
DateParseHandling=DateParseHandling.None,
};
}
公共类jsonDecode
{
publicstaticroot FromJson(stringJSON)=>JsonConvert.DeserializeObject(json/*,Converter.Settings*/);
公共部分类根
{
[J(“time”)]公共长时间{get;set;}
[J(“全局”)]公共全局{get;set;}
[J(“algos”)]公共列表algos{get;set;}
[J(“池”)]公共列表池{get;set;}
}
公共部分类算法
{
[J(“workers”)]public int-workers{get;set;}
[J(“hashrate”)]公共双hashrate{get;set;}
[J(“hashrateString”)]公共字符串hashrateString{get;set;}
}
公共部分类全局
{
[J(“workers”)]public int-workers{get;set;}
[J(“hashrate”)]公共长hashrate{get;set;}
}
公共部分类池
{
[J(“加密”)]公共列表加密{get;set;}
}
公共部分类密码
{
[J(“name”)]公共字符串名称{get;set;}
[J(“symbol”)]公共字符串符号{get;set;}
[J(“算法”)]公共字符串算法{get;set;}
[J(“poolStats”)]公共poolStats poolStats{get;set;}
[J(“块”)]公共块块{get;set;}
[J(“工人”)公共工人工人{get;set;}
[J(“hashrate”)]公共双hashrate{get;set;}
[J(“workerCount”)]公共长workerCount{get;set;}
[J(“hashrateString”)]公共字符串hashrateString{get;set;}
}
公共部分类街区
{
[J(“挂起”)]公共长挂起{get;set;}
[J(“已确认”)]公共长确认{get;set;}
[J(“孤立的”)]公共长孤立的{get;set;}
}
公共部分类PoolStats
{
[J(“有效共享”)]公共字符串有效共享{get;set;}
[J(“validBlocks”)]公共字符串validBlocks{get;set;}
[J(“invalidShares”)]公共字符串invalidShares{get;set;}
[J(“invalidRate”)]公共字符串invalidRate{get;set;}
[J(“totalPaid”)]公共字符串totalPaid{get;set;}
}
公共部分阶级工人
{
[J(“worker”)]公共列表worker{get;set;}
[J(“股份”)]公开双重股份{get;set;}
[J(“无效共享”)]公共长无效共享{get;set;}
[J(“hashrate”)]公共双hashrate{get;set;}
[J(“hashrateString”)]公共字符串hashrateString{get;set;}
}
公共部分阶级工人
{
[J(“股份”)]公开双重股份{get;set;}
[J(“无效共享”)]公共长无效共享{get;set;}
[J(“hashrate”)]公共双hashrate{get;set;}
[J(“hashrateString”)]公共字符串hashrateString{get;set;}
}
}

}
算法
工作者
已将属性命名为子属性,您无法将它们反序列化为
列表
,因为它们是
字典

使用这些类来反序列化:

public partial class Root
{
    [JsonPropertyAttribute("time")] public long Time { get; set; }
    [JsonPropertyAttribute("global")] public Global Global { get; set; }
    [JsonPropertyAttribute("algos")] public Dictionary<string, Algo> Algos { get; set; }
    [JsonPropertyAttribute("pools")] public Dictionary<string, Pool> Pools { get; set; }
}

public partial class Global
{
    [JsonPropertyAttribute("workers")] public int Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public long Hashrate { get; set; }
}

public partial class Algo
{
    [JsonPropertyAttribute("workers")] public int Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}

public partial class Pool
{
    [JsonPropertyAttribute("name")] public string Name { get; set; }
    [JsonPropertyAttribute("symbol")] public string Symbol { get; set; }
    [JsonPropertyAttribute("algorithm")] public string Algorithm { get; set; }
    [JsonPropertyAttribute("poolStats")] public PoolStats PoolStats { get; set; }
    [JsonPropertyAttribute("blocks")] public Blocks Blocks { get; set; }
    [JsonPropertyAttribute("workers")] public Dictionary<string, Worker> Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("workerCount")] public long WorkerCount { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}

public partial class Blocks
{
    [JsonPropertyAttribute("pending")] public long Pending { get; set; }
    [JsonPropertyAttribute("confirmed")] public long Confirmed { get; set; }
    [JsonPropertyAttribute("orphaned")] public long Orphaned { get; set; }
}

public partial class PoolStats
{
    [JsonPropertyAttribute("validShares")] public string ValidShares { get; set; }
    [JsonPropertyAttribute("validBlocks")] public string ValidBlocks { get; set; }
    [JsonPropertyAttribute("invalidShares")] public string InvalidShares { get; set; }
    [JsonPropertyAttribute("invalidRate")] public string InvalidRate { get; set; }
    [JsonPropertyAttribute("totalPaid")] public string TotalPaid { get; set; }
}
public partial class Worker
{
    [JsonPropertyAttribute("shares")] public double Shares { get; set; }
    [JsonPropertyAttribute("invalidshares")] public long Invalidshares { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}
公共部分类根目录
{
[JsonPropertyAttribute(“时间”)]公共长时间{get;set;}
[JsonPropertyAttribute(“全局”)]公共全局全局{get;set;}
[JsonPropertyAttribute(“algos”)]公共字典algos{get;set;}
[JsonPropertyAttribute(“池”)]公共字典池{get;set;}
}
公共部分类全局
{
[JsonPropertyAttribute(“workers”)]public int-workers{get;set;}
[JsonPropertyAttribute(“hashrate”)]公共长hashrate{get;set;}
}
公共部分类算法
{
[JsonPropertyAttribute(“workers”)]public int-workers{get;set;}
[JsonPropertyAttribute(“hashrate”)]公共双hashrate{get;set;}
[JsonPropertyAttribute(“hashrateString”)]公共字符串hashrateString{get;set;}
}
公共部分类池
{
[JsonPropertyAttribute(“name”)]公共字符串名称{get;set;}
[JsonPropertyAttribute(“符号”)]公共字符串符号{get;set;}
[JsonPropertyAttribute(“算法”)]公共字符串算法{get;set;}
[JsonPropertyAttribute(“poolStats”)]公共poolStats poolStats{get;set;}
[JsonPropertyAttribute(“块”)]公共块块{get;set;}
[JsonPropertyAttribute(“workers”)]公共字典worker{get;set;}
[JsonPropertyAttribute(“hashrate”)]公共双hashrate{get;set;}
[JsonPropertyAttribute(“workerCount”)]公共长工作计数{get;set;}
[JsonPropertyAttribute(“hashrateString”)]公共字符串hashrateString{get;set;}
}
公共部分类街区
{
[JsonPropertyAttribute(“挂起”)]公共长挂起{get;set;}
[JsonPropertyAttribute(“已确认”)]公共长确认{get;set;}
[JsonPropertyAttribute(“孤立”)]公共长孤立{get;set;}
}
公共部分类PoolStats
{
[JsonPropertyAttribute(“validShares”)]公共字符串validShares{get;set;}
[JsonPropertyAttribute(“validBlocks”)]公共字符串
public static class Serialize
{
    public static string ToJson(this jsonDecode.Root self)
    {
        return JsonConvert.SerializeObject(self, Converter.Settings);
    }
}

public class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
    };
}
public class jsonDecode
{

    public static Root FromJson(string json) => JsonConvert.DeserializeObject<Root>(json/*, Converter.Settings*/);


    public partial class Root
    {
        [J("time")] public long Time { get; set; }
        [J("global")] public Global Global { get; set; }
        [J("algos")] public List<Algos> Algos { get; set; }
        [J("pools")] public List<Pools> Pools { get; set; }

    }

    public partial class Algos
    {
        [J("workers")] public int Workers { get; set; }
        [J("hashrate")] public double Hashrate { get; set; }
        [J("hashrateString")] public string HashrateString { get; set; }
    }

    public partial class Global
    {
        [J("workers")] public int Workers { get; set; }
        [J("hashrate")] public long Hashrate { get; set; }
    }

    public partial class Pools
    {
        [J("crypto")] public List<Crypto> Crypto { get; set; }
    }

    public partial class Crypto
    {
        [J("name")] public string Name { get; set; }
        [J("symbol")] public string Symbol { get; set; }
        [J("algorithm")] public string Algorithm { get; set; }
        [J("poolStats")] public PoolStats PoolStats { get; set; }
        [J("blocks")] public Blocks Blocks { get; set; }
        [J("workers")] public Workers Workers { get; set; }
        [J("hashrate")] public double Hashrate { get; set; }
        [J("workerCount")] public long WorkerCount { get; set; }
        [J("hashrateString")] public string HashrateString { get; set; }
    }

    public partial class Blocks
    {
        [J("pending")] public long Pending { get; set; }
        [J("confirmed")] public long Confirmed { get; set; }
        [J("orphaned")] public long Orphaned { get; set; }
    }

    public partial class PoolStats
    {
        [J("validShares")] public string ValidShares { get; set; }
        [J("validBlocks")] public string ValidBlocks { get; set; }
        [J("invalidShares")] public string InvalidShares { get; set; }
        [J("invalidRate")] public string InvalidRate { get; set; }
        [J("totalPaid")] public string TotalPaid { get; set; }
    }

    public partial class Workers
    {
        [J("worker")] public List<Workers> Worker { get; set;  }
        [J("shares")] public double Shares { get; set; }
        [J("invalidshares")] public long Invalidshares { get; set; }
        [J("hashrate")] public double Hashrate { get; set; }
        [J("hashrateString")] public string HashrateString { get; set; }
    }


    public partial class Worker
    {
        [J("shares")] public double Shares { get; set; }
        [J("invalidshares")] public long Invalidshares { get; set; }
        [J("hashrate")] public double Hashrate { get; set; }
        [J("hashrateString")] public string HashrateString { get; set; }
    }





}
public partial class Root
{
    [JsonPropertyAttribute("time")] public long Time { get; set; }
    [JsonPropertyAttribute("global")] public Global Global { get; set; }
    [JsonPropertyAttribute("algos")] public Dictionary<string, Algo> Algos { get; set; }
    [JsonPropertyAttribute("pools")] public Dictionary<string, Pool> Pools { get; set; }
}

public partial class Global
{
    [JsonPropertyAttribute("workers")] public int Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public long Hashrate { get; set; }
}

public partial class Algo
{
    [JsonPropertyAttribute("workers")] public int Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}

public partial class Pool
{
    [JsonPropertyAttribute("name")] public string Name { get; set; }
    [JsonPropertyAttribute("symbol")] public string Symbol { get; set; }
    [JsonPropertyAttribute("algorithm")] public string Algorithm { get; set; }
    [JsonPropertyAttribute("poolStats")] public PoolStats PoolStats { get; set; }
    [JsonPropertyAttribute("blocks")] public Blocks Blocks { get; set; }
    [JsonPropertyAttribute("workers")] public Dictionary<string, Worker> Workers { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("workerCount")] public long WorkerCount { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}

public partial class Blocks
{
    [JsonPropertyAttribute("pending")] public long Pending { get; set; }
    [JsonPropertyAttribute("confirmed")] public long Confirmed { get; set; }
    [JsonPropertyAttribute("orphaned")] public long Orphaned { get; set; }
}

public partial class PoolStats
{
    [JsonPropertyAttribute("validShares")] public string ValidShares { get; set; }
    [JsonPropertyAttribute("validBlocks")] public string ValidBlocks { get; set; }
    [JsonPropertyAttribute("invalidShares")] public string InvalidShares { get; set; }
    [JsonPropertyAttribute("invalidRate")] public string InvalidRate { get; set; }
    [JsonPropertyAttribute("totalPaid")] public string TotalPaid { get; set; }
}
public partial class Worker
{
    [JsonPropertyAttribute("shares")] public double Shares { get; set; }
    [JsonPropertyAttribute("invalidshares")] public long Invalidshares { get; set; }
    [JsonPropertyAttribute("hashrate")] public double Hashrate { get; set; }
    [JsonPropertyAttribute("hashrateString")] public string HashrateString { get; set; }
}