简体   繁体   English

反序列化JSON以填充数组并对值进行计算

[英]Deserialize JSON to populate array and make calculations on values

Good evening everyone, 各位晚上好,

I'm working on an application that have retrive some JSON values (ex 0.00004582) from an URL (this for example https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NEO&tickInterval=day ) with Microsoft Visual C# 2017. 我正在使用从URL(例如https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-NEO&tickInterval=天 ))与Microsoft Visual C#2017。

I'm able to read the URL, and i set up a class to define how the JSON value have to be read, my logic suggests that i have to store them in a variables and then make calculation with them, but the problem is that i can't make the program read the values correctly, and i don't know how to use the objects to make my calculations. 我能够读取URL,并且我设置了一个类来定义必须如何读取JSON值,我的逻辑建议我必须将它们存储在变量中,然后使用它们进行计算,但是问题是我无法使程序正确读取值,并且我不知道如何使用对象进行计算。

This is the code of the Data.cs class 这是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; }
    }
}

While this is the code from the Form1 这是来自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);
            }
        }
    }
}

What can i do? 我能做什么? Can you point me out to some good resource to understand what to do? 您能指出一些有用的资源来了解该怎么做吗?

Thank you very much. 非常感谢你。

Your class for serialization is slightly wrong, it should look more like this: 您的序列化类有点错误,应该看起来像这样:

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

Then the line for serialization should read: 然后,用于序列化的行应为:

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

Then to iterate the results it would look like this: 然后要对结果进行迭代,如下所示:

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

Output: 输出:

0.000264 0.000264
0.0001887 0.0001887
0.000227 0.000227
0.00022576 0.00022576
0.00025723 0.00025723

Admittedly you might want to rename some of the classes, result in results.result is confusing but that's up to you. 诚然,您可能想重命名某些类, result in results.result令人困惑,但这取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM