2017-09-04 21 views
1

大家晚上好,反序列化JSON填充陣列,並從URL進行計算的值

我的工作有一些retrive JSON值的應用程序(例如0.00004582)(此例如https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NEO&tickInterval=day)與Microsoft Visual C#2017.

我能夠讀取URL,並且我設置了一個類來定義JSON值是如何讀取的,我的邏輯建議我必須將它們存儲在變量中,然後使用他們,但問題是我不能讓程序正確讀取值,並且我不知道如何使用這些對象來進行我的計算。

這是Data.cs的代碼

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

namespace WindowsFormsApp3 
{ 
    public class Data 
    { 
     internal IEnumerable<object> data; 

     public double O { get; set; } 
     public double H { get; set; } 
     public double L { get; set; } 
     public double C { get; set; } 
     public double V { get; set; } 
     public string T { get; set; } 
     public double BV { get; set; } 
    } 

    public class RootObject 
    { 
     public bool success { get; set; } 
     public string message { get; set; } 
     public List<Result> result { get; set; } 
    } 
} 

雖然這是從Form1的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Net; 
using Newtonsoft.Json; 

namespace WindowsFormsApp3 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      var client = new WebClient(); 
      var text = client.DownloadString("https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NEO&tickInterval=day"); 
      var result = JsonConvert.DeserializeObject<Data>(text); 
      foreach (var data in result.C) 
      { 
       Console.WriteLine(result); 
      } 
     } 
    } 
} 

代碼我能做些什麼?你能否指點我一些很好的資源來理解要做什麼?

非常感謝。

回答

0

您的序列化類是稍有不當,就應該看起來更像是這樣的:

public class Result 
{ 
    public double O { get; set; } 
    public double H { get; set; } 
    public double L { get; set; } 
    public double C { get; set; } 
    public double V { get; set; } 
    public string T { get; set; } 
    public double BV { get; set; } 
} 

public class RootObject 
{ 
    public bool success { get; set; } 
    public string message { get; set; } 
    public List<Result> result { get; set; } 
} 

然後序列化該行應爲:

var results = JsonConvert.DeserializeObject<RootObject>(text); 

然後遍歷結果它會是什麼樣子這個:

foreach (var result in results.result) 
{ 
    Console.WriteLine(result.C); 
} 

輸出:

0.000264
0.0001887
0.000227
0.00022576
0.00025723

你可能要重新命名一些類不可否認,result in results.result是混亂,但一切由你。

+0

感謝您的回覆!正是我在找什麼。 您可以請告訴我您認爲這是對這類數據進行計算的最佳方式嗎? – Revengeic3