0

I have a nested json string, instead of using arrays the next level is another json structure. This creates a mess of deserializing using traditional methods.

Most other answers dealing with parsing json have clearly defined structures, and in most cases can be solved using online tools such as http://json2csharp.com/. But because this JSON doesn't use arrays properly I'm having trouble coming up with a solution to deserialize it.

For example:

 {
   "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"
      }
   }
}

I'm having trouble deserializing these two parts specifically:

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

},

"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"
        }
     },

The Code I've Tried

namespace pooldecode

{

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; }
    }





}

}

3
  • Please post the code in C# that you have tried that is not working. Commented Jan 24, 2018 at 21:06
  • 1
    @codeMonkey added Commented Jan 24, 2018 at 21:08
  • Use Json.NET. Dont inline your attributes like that, its really unreadable. What exactly is the problem with your serializing/deserializing? There are no arrays here, its just an object structure. What specific issues are you having? Commented Jan 24, 2018 at 21:15

1 Answer 1

2

Algos, Pools and Workers have named properties as childrens, you can't deserialize them as List<T> since they are Dictionary<string, T>,

Use these classes to deserialize:

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@maccettura It is just a simple find/replace of OP's code, I didn't bother further formatting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.