2017-09-15 135 views
0

我有了反序列化包裹在“结果”根对象数据的阵列,使用Netwonsoft.Json包从的NuGetC#接收JSON字符串,但无法反序列化

应用JSON字符串是正是这种:

{"results":[{"Coin":"SBD","LP":0.000269,"PBV":-54.36,"MACD1M":true,"MACD30M":true,"MACD1H":true,"MACD1D":true},{"Coin":"XMR","LP":0.027135,"PBV":11.44,"MACD1M":true,"MACD30M":true,"MACD1H":true,"MACD1D":true}]} 

这JSON字符串是从一个控制台应用程序我做创建的,我希望它看起来像这样https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NEO&tickInterval=hour

我的类看起来像这样

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WindowsFormsApp2 
{ 
    public class Result 
    { 
     public string Coins { get; set; } 
     public decimal LastPrice { get; set; } 
     public decimal PercentBuyVolume { get; set; } 
    } 

    public class RootObject 
    { 
     public List<Result> results { get; set; } 
    } 
} 

在主窗体中,我有一个函数可以从JSON(我有运行Apache的XAMPP)的URL下载并将其反序列化到一个数组中。它看起来像这样:

private void DownloadBittrexData() 
     { 

      int PanelID = 0; 
      var Coin = new List<string>(); 
      var LastPrice = new List<decimal>(); 
      var PercentBuyVolume = new List<decimal>(); 
      var MACD1M = new List<bool>(); 
      var MACD30M = new List<bool>(); 
      var MACD1H = new List<bool>(); 
      var MACD1D = new List<bool>(); 

      var client = new WebClient(); 

      var URL = client.DownloadString("http://localhost/test.json"); 
      Console.WriteLine("Json String from URL: " + URL); 
      var dataDeserialized = JsonConvert.DeserializeObject<RootObject>(URL); 
      foreach (var data in dataDeserialized.results) 
      { 
       Coin.Add(data.Coins); 
       LastPrice.Add(data.LastPrice); 
       PercentBuyVolume.Add(data.PercentBuyVolume); 

      } 
      int sizeOfArrayClose = Coin.Count - 1; 

      for (int i = 0; i <= sizeOfArrayClose; i++) 
      { 
       Console.WriteLine("Coin: " + Coin[i]); 
       Console.WriteLine("Lastprice: " + LastPrice[i]); 
       Console.WriteLine("PBV: " + PercentBuyVolume[i]); 
      } 
     } 

Newtonsoft.Json是在窗体的开头System.Net一起申报当然

using System.Net; 
using Newtonsoft.Json; 

输出看起来是这样的:

Json String from URL: {"results":[{"Coin":"SBD","LP":0.000269,"PBV":-54.36,"MACD1M":true,"MACD30M":true,"MACD1H":true,"MACD1D":true},{"Coin":"XMR","LP":0.027135,"PBV":11.44,"MACD1M":true,"MACD30M":true,"MACD1H":true,"MACD1D":true}]} 
Coin: 
Lastprice: 0 
PBV: 0 
Coin: 
Lastprice: 0 
PBV: 0 

这就像下载它后无法反序列化它。 我该怎么办?非常感谢你。

+0

json是正确的。你得到哪个错误? –

+2

在该JSON字符串中,没有称为LastPrice和PercentByVolume的字段。你有没有显示出正在处理的映射代码? –

+0

不,但我想我得到了解决方案,这要感谢David Watts。 我需要在'public Class Result {}内调用对象,就像在Json字符串中调用它们一样。 Infact现在输出正确! JSON String URL:{“results”:[{“Coin”:“SBD”,“LP”:0.000269,“PBV”: - 54.36,“MACD1M”:true,“MACD30M”:true,“MACD1H “:真正的” MACD1D “:真正},{” 硬币 “:” XMR”, “LP”:0.027135, “PBV”:11.44, “MACD1M”:真实的, “MACD30M”:真实的, “MACD1H”:真实, “MACD1D”:真正}]} 硬币:SBD Lastprice:0,000269 PBV:-54,36 硬币:XMR Lastprice:0,027135 PBV:11,44' 感谢@DavidWattsË感恩教堂mille @Piero Alberto – Revengeic3

回答

0

我想我的系统中您确切的代码,我能按预期检索结果。希望这有帮助,这很容易理解。

这里是主类

static void Main(string[] args) 
    { 

     RootObject configfile = LoadJson(); 

     foreach (var tResult in configfile.results) 
     { 
      Console.WriteLine("Coin: " + tResult.Coin); 
      Console.WriteLine("Lastprice: " + tResult.LP); 
      Console.WriteLine("PBV: " + tResult.PBV); 
     } 


     Console.ReadLine(); 

    } 

LoadJson功能将

private static RootObject LoadJson() 
    { 
     string json = "{\"results\":[{\"Coin\":\"SBD\",\"LP\":0.000269,\"PBV\":-54.36,\"MACD1M\":true,\"MACD30M\":true,\"MACD1H\":true,\"MACD1D\":true},{\"Coin\":\"XMR\",\"LP\":0.027135,\"PBV\":11.44,\"MACD1M\":true,\"MACD30M\":true,\"MACD1H\":true,\"MACD1D\":true}]}"; 



     RootObject configs = Deserialize<RootObject>(json); 
     return configs; 
    } 

和反序列化功能将是

private static T Deserialize<T>(string json) 
    { 
     T unsecureResult; 
     string _DateTypeFormat = "yyyy-MM-dd HH:mm:ss"; 
     DataContractJsonSerializerSettings serializerSettings = new DataContractJsonSerializerSettings(); 
     DataContractJsonSerializer serializer; 
     MemoryStream ms; 
     unsecureResult = default(T); 
     serializerSettings.DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat(_DateTypeFormat); 

     serializer = new DataContractJsonSerializer(typeof(T)); 
     ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); 

     unsecureResult = (T)serializer.ReadObject(ms); 

     return unsecureResult; 
    } 

,现在你的数据模型将

public class Result 
{ 

    public string Coin { get; set; } 
    public double LP { get; set; } 
    public double PBV { get; set; } 
    public bool MACD1M { get; set; } 
    public bool MACD30M { get; set; } 
    public bool MACD1H { get; set; } 
    public bool MACD1D { get; set; } 

} 

public class RootObject 
{ 
    public List<Result> results { get; set; } 
} 
1

模型应该是这样的反序列化JSON

public class Result 
{ 
    public string Coin { get; set; } 
    public double LP { get; set; } 
    public double PBV { get; set; } 
    public bool MACD1M { get; set; } 
    public bool MACD30M { get; set; } 
    public bool MACD1H { get; set; } 
    public bool MACD1D { get; set; } 
} 

public class RootObject 
{ 
    public List<Result> results { get; set; } 
} 

LastPrice and PercentBuyVolume不是你的模型这就是它得到一个错误的原因提供。

2

您的属性名称不会映射到JSON中的字段名称。您可以重命名您的C#属性以匹配JSON,但它会导致无法读取的下游代码。

相反,你应该你的属性(与漂亮的,可读的名称)映射到出现在JSON的名称,使用JsonPropertyAttribute

public class Result 
{ 
    public string Coin { get; set; } //didn't bother here: changed property name to Coin 
    [JsonProperty("LP")] 
    public decimal LastPrice { get; set; } 
    [JsonProperty("PBV")] 
    public decimal PercentBuyVolume { get; set; } 
} 
+0

这是我对这个问题发表评论的观点。我不确定的一件事是,如果套管很重要,那么硬币也需要一个吗?我只处理过CamelCase JSON属性 –

+0

@DavidWatts嗯,在这种情况下,'Coin'映射到'Coin'并且外壳被保留。 AFAIK,JSON.net首先尝试区分大小写的映射,但如果这会导致空白,则会再次尝试使用不区分大小写的映射。由所有帐户,这是不可配置的:https://stackoverflow.com/a/34637862/14357 – spender

+0

感谢您的信息:) –

相关问题