为什么我的软件说数据是= 0(null) [英] Why is my software saying that the data is = 0 (null)

查看:107
本文介绍了为什么我的软件说数据是= 0(null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好再次!



我修复了我在没有任何帮助的情况下使用这个软件的最后一个问题,但现在告诉我一些我以前从未发生过的事情。 br />


让我告诉你代码,这样就可以更轻松地解决这个问题。



Hello once again!

I fixed the last issue I had with this software without any help but now its telling me something that I have never occurd before.

Let me show you the code so it will be easier toe xplain the situation.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using RestSharp;

namespace BitCoin_Evolution
{

    public partial class Form1 : Form
    {
        string con = "Connected";
        string dc = "Disconnected";

        private Timer timer1;
        int togMove;
        int MvalX;
        int MvalY;

        public Form1()
        {
            
            InitializeComponent();
            timer1 = new Timer();
            timer1.Tick += new EventHandler(Update_BTC_Ticker);
            timer1.Tick += new EventHandler(Update_BTC_Trader);
            //timer1.Tick += new EventHandler(Update_BTC_Trades);
            timer1.Interval = 2000;
            timer1.Start();
        }

        private void Update_BTC_Ticker(object sender, EventArgs e)
        {
            var client = new RestClient("https://btc-e.com/api");
            var request = new RestRequest("2/btc_usd/ticker", Method.GET);

            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcUsdTicker> response = client.Execute<BtcUsdTicker>(request);

            sellLabel.Text = Convert.ToString(response.Data.ticker.sell);
            buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy);
        }






        private void Update_BTC_Trader(object sender, EventArgs e)
        {

            var tradeClient = new RestClient("https://btc-e.com/api");
            var tradeRequest = new RestRequest("2/btc_usd/trades", Method.GET);
            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            tradeRequest.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcTradeTicker> response = tradeClient.Execute<BtcTradeTicker>(tradeRequest);

            textBox1.Text = Convert.ToString(response.Data.tradeTicker.price);





        }

        public class BtcUsdTicker
        {
            public Ticker ticker { get; set; }
        }

        public class Ticker
        {

            public float high { get; set; }
            public float low { get; set; }
            public float avg { get; set; }
            public float vol { get; set; }
            public float vol_cur { get; set; }
            public float last { get; set; }
            public float buy { get; set; }
            public float sell { get; set; }
            public int updated { get; set; }
            public int server_time { get; set; }


        }

        
        public class BtcTradeTicker
        {
            public tradeTicker tradeTicker { get; set; }
        }


        public class tradeTicker
        {
            public int date { get; set; }
            public double price { get; set; }
            public double amount { get; set; }
            public int tid { get; set; }
            public string price_currency { get; set; }
            public string item { get; set; }
            public string trade_type { get; set; }
        }







让我解释一下发生了什么。



我调用网络请求






let me explain what is going on.

I cam calling a webrequest to

var client = new RestClient("https://btc-e.com/api");
var request = new RestRequest("2/btc_usd/ticker", Method.GET);



哪个工作正常,连接标签并打印出来没有任何麻烦。




Which is working perfectly, its connecting with the labels and printing out without any hassle.

sellLabel.Text = Convert.ToString(response.Data.ticker.sell);
buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy);










private void Update_BTC_Ticker(object sender, EventArgs e)
{
    var client = new RestClient("https://btc-e.com/api"); //1
    var request = new RestRequest("2/btc_usd/ticker", Method.GET); //2

    //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
    request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; //3

    IRestResponse<BtcUsdTicker> response = client.Execute<BtcUsdTicker>(request); //4

    sellLabel.Text = Convert.ToString(response.Data.ticker.sell); //5
    buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy); //6
}







正如你所看到的那样,1是它的主要网站,然后我给它提供了它需要从这个精确的APi拉出来的额外信息(数字2)



(3)然后它将代码desierializes为Json> C#

(4)它调用它刚刚反序列化的内容来检查那里是否有东西(正如你在代码和API中看到的那样有数据)



(5)它找到卖出的一个并将值返回到标签




As you can see the 1 is the main website its pulling from and then I am giving it the extra information that it needs to be pulling from this exact APi (number 2)

(3)Then it desierializes the code to Json > C#
(4) its calling what it just deserialized to check if there is something there (as you can see in the code and the API there is Data there)

(5) its locating the sell one and returning the value to the label

public float sell { get; set; }





(6)它与(5)完全相同,但它拉动.buy而不是.sell





我的问题是..



为什么它不从其他数据中提取数据我创建的json表





(6) It does the exact same thing as (5) but its pulling .buy instead of .sell


My question is..


How come its not pulling the data from the other json table I created

public class BtcTradeTicker





它有



It has the

public double price { get; set; }



用于获取和检查API,而不是说.Data为空




For it to grab and check with the API but instead its saying that .Data is null

textBox1.Text = Convert.ToString(response.Data.tradeTicker.price);







Sidenote,我使用名为restSharp的第三方库



RestSharp - 用于.NET的简单REST和HTTP客户端 [ ^ ]



我尝试了什么:



代码在开始时看起来不像那样,我我多次查看API和我的代码我真的找不到什么错误,我基本上试图重做我为第一次webrequest所做的完全相同的事情,但第二次使用不同的API,但它不让我获得任何数据。




Sidenote, I am using a third party library called restSharp

RestSharp - Simple REST and HTTP Client for .NET[^]

What I have tried:

The code didnt look like that in the beginning, I've looked over the API and my code multiple times I really cant find whats wrong, I am bascally trying to redo the exact same thing I did for the first webrequest but a second time for a different API but its not letting me get any Data.

推荐答案

我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于为什么我的软件说数据是= 0(null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆