Json Api值为0或false [英] Json Api values 0 or false
问题描述
我正在使用Web JSON api将来自游戏市场的一些价值提供给c#对象.我对C#相当陌生,并且以前从未使用过API.
I am using a web JSON api to provide some values from a game market into c# objects. I am fairly new to c# and haven't worked with API's before.
这里是我的代码:
using System;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace HttpsApiTest
{
class Program
{
public class ForQuery
{
public static bool bid { get; set; }
public static List<int> types { get; set; }
public static List<object> regions { get; set; }
public static List<object> systems { get; set; }
public static int hours { get; set; }
public static int minq { get; set; }
}
public class Buy
{
public static ForQuery forQuery { get; set; }
public static long volume { get; set; }
public static double wavg { get; set; }
public static double avg { get; set; }
public static double variance { get; set; }
public static double stdDev { get; set; }
public static double median { get; set; }
public static double fivePercent { get; set; }
public static double max { get; set; }
public static double min { get; set; }
public static bool highToLow { get; set; }
public static long generated { get; set; }
}
public class ForQuery2
{
public static bool bid { get; set; }
public static List<int> types { get; set; }
public static List<object> regions { get; set; }
public static List<object> systems { get; set; }
public static int hours { get; set; }
public static int minq { get; set; }
}
public class Sell
{
public ForQuery2 forQuery { get; set; }
public static int volume { get; set; }
public static double wavg { get; set; }
public static double avg { get; set; }
public static double variance { get; set; }
public static double stdDev { get; set; }
public static double median { get; set; }
public static double fivePercent { get; set; }
public static double max { get; set; }
public static double min { get; set; }
public static bool highToLow { get; set; }
public static long generated { get; set; }
}
public class RootObject
{
public Buy buy { get; set; }
public Sell sell { get; set; }
}
static void Main(string[] args)
{
string sURL = "https://api.evemarketer.com/ec/marketstat/json?typeid=1230®ionlimit=10000002";
StreamReader objReader = new StreamReader(WebRequest.Create(sURL).GetResponse().GetResponseStream());
string sLine = objReader.ReadLine();
JToken.Parse(sLine.Replace("[", "").Replace("]", "")).ToObject<RootObject>();
Console.WriteLine(Buy.max);
Console.WriteLine(Buy.highToLow);
Console.ReadLine();
}
}
}
和Web api JSON输出以下内容:
and the web api JSON outputs this:
[
{
"buy": {
"forQuery": {
"bid": true,
"types": [
1230
],
"regions": [],
"systems": [],
"hours": 24,
"minq": 1
},
"volume": 5544790080,
"wavg": 11.43,
"avg": 10.86,
"variance": 4.38,
"stdDev": 2.09,
"median": 12,
"fivePercent": 13.75,
"max": 20,
"min": 5,
"highToLow": true,
"generated": 1551926105235
},
"sell": {
"forQuery": {
"bid": false,
"types": [
1230
],
"regions": [],
"systems": [],
"hours": 24,
"minq": 1
},
"volume": 258207299,
"wavg": 16.8,
"avg": 20.21,
"variance": 132.71,
"stdDev": 11.52,
"median": 15.99,
"fivePercent": 12.92,
"max": 60,
"min": 6,
"highToLow": false,
"generated": 1551926105235
}
}
]
我不确定为什么Console.WriteLine(Buy.max);
显示为0而不是20,为什么Console.WriteLine(Buy.highToLow);
显示为false而不是true.我究竟做错了什么?在过去的几个小时里,我一直在寻找解决该问题的方法,但无济于事.任何帮助将不胜感激!
I am not sure why Console.WriteLine(Buy.max);
shows as 0 instead of 20 and Console.WriteLine(Buy.highToLow);
shows as false instead of true. what am I doing wrong? I have looked for solutions to this issue for the past few hours to no avail. Any help would be greatly appreciated!
推荐答案
他们说的话:
- 静态成员通常用于帮助程序或工厂方法或常量
- API返回一个数组(或列表)
- 这种字符串foo替换充其量只是粗略的.任何时候使用JSON或XML之类的标准时,都可以使用库将其转换为对象,然后进行操作
这是一个有效的代码段:
Here's a working snippet:
using System;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace HttpsApiTest
{
class Program
{
public class ForQuery
{
public bool bid { get; set; }
public List<int> types { get; set; }
public List<object> regions { get; set; }
public List<object> systems { get; set; }
public int hours { get; set; }
public int minq { get; set; }
}
public class Buy
{
public ForQuery forQuery { get; set; }
public long volume { get; set; }
public double wavg { get; set; }
public double avg { get; set; }
public double variance { get; set; }
public double stdDev { get; set; }
public double median { get; set; }
public double fivePercent { get; set; }
public double max { get; set; }
public double min { get; set; }
public bool highToLow { get; set; }
public long generated { get; set; }
}
public class ForQuery2
{
public bool bid { get; set; }
public List<int> types { get; set; }
public List<object> regions { get; set; }
public List<object> systems { get; set; }
public int hours { get; set; }
public int minq { get; set; }
}
public class Sell
{
public ForQuery2 forQuery { get; set; }
public int volume { get; set; }
public double wavg { get; set; }
public double avg { get; set; }
public double variance { get; set; }
public double stdDev { get; set; }
public double median { get; set; }
public double fivePercent { get; set; }
public double max { get; set; }
public double min { get; set; }
public bool highToLow { get; set; }
public long generated { get; set; }
}
public class RootObject
{
public Buy buy { get; set; }
public Sell sell { get; set; }
}
static void Main(string[] args)
{
string sURL = "https://api.evemarketer.com/ec/marketstat/json?typeid=1230®ionlimit=10000002";
StreamReader objReader = new StreamReader(WebRequest.Create(sURL).GetResponse().GetResponseStream());
string sLine = objReader.ReadLine();
var obj = JToken.Parse(sLine).ToObject<List<RootObject>>();
Console.WriteLine("Buy node:");
Console.WriteLine(" Max: " + obj[0].buy.max);
Console.WriteLine(" HighToLow: " + obj[0].buy.highToLow);
Console.WriteLine("Sell node:");
Console.WriteLine(" Max: " + obj[0].sell.max);
Console.WriteLine(" HighToLow: " + obj[0].sell.highToLow);
Console.ReadLine();
}
}
}
这篇关于Json Api值为0或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!