Json String不想反序列化

时间:2015-10-20 23:48:21

标签: c# json

我坚持使用json反序列化,似乎有些东西我无法抓住这个json:

{
    "success" : true,
    "message" : "",
    "result" : [{
            "Currency" : "DOGE",
            "Balance" : 0.00000000,
            "Available" : 0.00000000,
            "Pending" : 0.00000000,
            "CryptoAddress" : "DLxcEt3AatMyr2NTatzjsfHNoB9NT62HiF",
            "Requested" : false,
            "Uuid" : null

        }, {
            "Currency" : "BTC",
            "Balance" : 14.21549076,
            "Available" : 14.21549076,
            "Pending" : 0.00000000,
            "CryptoAddress" : "1Mrcdr6715hjda34pdXuLqXcju6qgwHA31",
            "Requested" : false,
            "Uuid" : null
        }
    ]
}

以下是我正在尝试的代码:

       if (stream == null) return account;
        var re = new StreamReader(stream);
        var json = re.ReadToEnd();
        var jobj = (JObject)JsonConvert.DeserializeObject(json);
        var items = jobj.Children()
            .Cast<JProperty>()
            .Select(j => new
            {
                ID = j.Name,
                Other = (string)j.Value["Currency"]
            })
            .ToList();

但不幸的是,这段代码没有任何反应,而且我为什么会这么做。在这里搜索之后,尝试在类似线程上找到的其他几个解决方案,这个是我能够最接近我的字符串去除。

我有两节课,我希望用这个json填写:

public class AccountCurrencies
{
    public string Jsonid { get; set; }
    public string Currency { get; set; }
    public double Balance { get; set; }
    public double Available { get; set; }
    public double Pending { get; set; }
    public string CryptoAddress { get; set; }
    public bool Requested { get; set; }
    public object Uuid { get; set; }
}

public class AccountWrapper
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public List<AccountCurrencies> AccountAllCurrencies  { get; set; }
}

1 个答案:

答案 0 :(得分:1)

你在这里缺少两件事。首先让Json进入你的类结构是一行代码:

var yourObject = JsonConvert.DeserializeObject<AccountWrapper>(json);

其次,您需要告诉序列化器在Json数据中AccountAllCurrencies属性被称为“result”。为此,请使用JsonProperty属性:

public class AccountWrapper
{
    public bool Success { get; set; }
    public string Message { get; set; }

    //Add this line
    [JsonProperty("result")]
    public List<AccountCurrencies> AccountAllCurrencies  { get; set; }
}