首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#解析不使用JSON数组的嵌套JSON

c#解析不使用JSON数组的嵌套JSON
EN

Stack Overflow用户
提问于 2018-01-24 20:59:53
回答 1查看 158关注 0票数 0

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

处理解析json的大多数其他答案都有明确定义的结构,而且在大多数情况下可以使用在线工具(如http://json2csharp.com/ )来解决。但是由于这个JSON没有正确地使用数组,所以我想不出一个反序列化它的解决方案。

例如:

代码语言:javascript
复制
 {
   "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"
      }
   }
}

我在反序列化这两个部分时遇到了困难,特别是:

代码语言:javascript
复制
"algos":{
  "scrypt-n":{
     "workers":1,
     "hashrate":79752.92436043094,
     "hashrateString":"79.75 KH"
  }

},

代码语言:javascript
复制
"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"
        }
     },

--我试过的代码

代码语言:javascript
复制
namespace pooldecode

{

代码语言:javascript
复制
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; }
    }





}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-24 21:21:50

AlgosPoolsWorkers将属性命名为子属性,您不能将它们反序列化为List<T>,因为它们是Dictionary<string, T>

使用这些类反序列化:

代码语言:javascript
复制
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; }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48431495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档