【问题标题】:C#.NET Deserialize JSON string to objectC#.NET 将 JSON 字符串反序列化为对象
【发布时间】:2016-06-26 07:24:44
【问题描述】:

我正在尝试将 JSON 反序列化为对象“MarginBalance”。当我尝试反序列化这个 JSON 时:

{"totalValue":"0.00091979","pl":"0.00000000","lendingFees":"0.00000000","netValue":"0.00091979","totalBorrowedValue":"0.00000000","currentMargin":"1.00000000"}

到这个对象:

public class MarginBalance : IMarginBalance
{ 
    [JsonProperty("totalValue")]
    public double TotalValue { get; set; }

    [JsonProperty("pl")]
    public double PL { get; set; }

    [JsonProperty("lendingFees")]
    public double LendingFees { get; set; }

    [JsonProperty("netValue")]
    public double NetValue { get; set; }

    [JsonProperty("totalBorrowedValue")]
    public double TotalBorrowedValue { get; set; }

    [JsonProperty("currentMargin")]
    public double CurrentMargin { get; set; }
}

实现这个接口:

public interface IMarginBalance
{
    double TotalValue { get; } 
    double PL { get; } 
    double LendingFees { get; } 
    double NetValue { get; } 
    double TotalBorrowedValue { get; } 
    double CurrentMargin { get; }
}

它返回空值。这是我的反序列化代码:

var postData = new Dictionary<string, object>();
var data = PostData<IDictionary<string, MarginBalance>>("returnMarginAccountSummary", postData);
if (data != null)
{
    // never reaches here 
    var returnData = new Dictionary<string, IMarginBalance>();
    foreach (string key in data.Keys)
    {
        returnData.Add(key, data[key]);
    }
    return returnData;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T PostData<T>(string command, Dictionary<string, object> postData)
{
    return ApiWebClient.PostData<T>(command, postData);
} 

public T PostData<T>(string command, Dictionary<string, object> postData)
{
    postData.Add("command", command);
    postData.Add("nonce", Helper.GetCurrentHttpPostNonce());

    var jsonString = PostString(Helper.ApiUrlHttpsRelativeTrading, postData.ToHttpPostString());
    var output = JsonSerializer.DeserializeObject<T>(jsonString);

    return output;
}

希望有人能解决!我一直在尝试一整天...在此先感谢。

【问题讨论】:

  • 向我们展示更多代码。你如何进行反序列化?
  • @dotctor 我更新了我的问题
  • JsonSerializer.DeserializeObject更改为JsonConvert.DeserializeObject
  • @dotctor 当我更改为 JsonConvert 时收到此错误:Newtonsoft.Json.dll 中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理附加信息:转换错误值“0.00091977”以键入“Jojatekok.PoloniexAPI.WalletTools.MarginBalance”。路径“totalValue”,第 1 行,位置 26。

标签: c# json json.net json-deserialization


【解决方案1】:

Json 中的数字被撇号包围,表示它们是字符串,而不是数字。您可以尝试从 Json 字符串中的数字中删除撇号,或者将字段类型从 double 更改为对象中的字符串。

您在MarginBalance 中使用的JsonProperty 属性属于Newtonsoft.Json.Serialization 命名空间。要反序列化对象,您应该使用JsonConvert.DeserializeObject&lt;MarginBalance&gt;(jsonString)

【讨论】:

  • 我试过了,收到了 Newtonsoft.Json.dll 中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理附加信息:将值“0.00091977”转换为“类型”时出错Jojatekok.PoloniexAPI.WalletTools.MarginBalance'。路径“totalValue”,第 1 行,位置 26。
  • @Nickmccomb 你仍在反序列化为IDictionary&lt;string, MarginBalance&gt;。如果您只有一个 MarginBalance,请不要这样做。
  • Json 中的数字被撇号包围,表示它们是字符串,而不是数字。您可以尝试从 Json 字符串中的数字中删除撇号,或者将字段类型从 double 更改为对象中的字符串。
  • @hvd - 是的,我也认为是这样,只需更改我的代码,如果可行,我会将您的答案标记为完整:)
  • @Nickmccomb 这个答案已经有 JsonConvert.DeserializeObject&lt;MarginBalance&gt; 了(在我编辑它之前不可读,但它在那里),所以我认为接受这个答案是可以的。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
  • 2021-04-27
  • 2019-05-30
  • 1970-01-01
相关资源
最近更新 更多