C# Json Api值为0或false
我使用一个web JSON api将一些值从游戏市场提供给c#对象。我对c#相当陌生,以前从未使用过API 这是我的密码:C# Json Api值为0或false,c#,json,parsing,console-application,C#,Json,Parsing,Console Application,我使用一个web JSON api将一些值从游戏市场提供给c#对象。我对c#相当陌生,以前从未使用过API 这是我的密码: using System; using System.Net; using System.IO; using Newtonsoft.Json.Linq; using System.Collections.Generic; namespace HttpsApiTest { class Program { public class ForQu
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();
}
}
}
我不知道为什么
Console.WriteLine(Buy.max)代码>显示为0而不是20和Console.WriteLine(Buy.highToLow)代码>显示为false而不是true。我做错了什么?在过去的几个小时里,我一直在寻找解决这一问题的办法,但毫无结果。任何帮助都将不胜感激 字符串替换将删除方括号的所有实例。最好将响应反序列化为RootObjects列表 类数据
class Data
{
public string DataPoint;
}
class CustomData
{
public Data Dp;
}
class Utility
{
public T JsonDeserialisation<T>(string jsonFile)
{
TextReader textReader = new StreamReader(jsonFile);
JsonTextReader jsonReader = new JsonTextReader(textReader);
return JsonSerializer.CreateDefault().Deserialize<T>(jsonReader);
}
}
{
公共字符串数据点;
}
类自定义数据
{
公共数据Dp;
}
类效用
{
公共JSONDESerialization(字符串jsonFile)
{
TextReader TextReader=新的StreamReader(jsonFile);
JsonTextReader jsonReader=新的JsonTextReader(文本阅读器);
返回JsonSerializer.CreateDefault().Deserialize(jsonReader);
}
}
由于您事先拥有所有类,因此可以使用以下代码json反序列化器。这里的“T”是CustomData类他们说的:
- 静态成员通常用于助手、工厂方法或常量
- API正在返回数组(或列表)
- 字符串foo的替换充其量只是粗略的。无论何时使用JSON或XML等标准,都可以使用库将其转换为对象,然后进行操作
下面是一个工作片段:
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();
}
}
}
使用系统;
Net系统;
使用System.IO;
使用Newtonsoft.Json.Linq;
使用System.Collections.Generic;
命名空间HttpsApiTest
{
班级计划
{
用于查询的公共类
{
公共布尔标{get;set;}
公共列表类型{get;set;}
公共列表区域{get;set;}
公共列表系统{get;set;}
公共整数小时数{get;set;}
公共int minq{get;set;}
}
公共类购买
{
public ForQuery ForQuery{get;set;}
公共长卷{get;set;}
公共双wavg{get;set;}
公共双平均值{get;set;}
公共双方差{get;set;}
公共双stdDev{get;set;}
公共双中位数{get;set;}
公共双五百分比{get;set;}
公共双最大值{get;set;}
公共双最小值{get;set;}
公共bool highToLow{get;set;}
公共长生成{get;set;}
}
查询2的公共类
{
公共布尔标{get;set;}
公共列表类型{get;set;}
公共列表区域{get;set;}
公共列表系统{get;set;}
公共整数小时数{get;set;}
公共int minq{get;set;}
}
公开课出售
{
public ForQuery2 forQuery{get;set;}
公共int卷{get;set;}
公共双wavg{get;set;}
公共双平均值{get;set;}
公共双方差{get;set;}
公共双stdDev{get;set;}
公共双中位数{get;set;}
公共双五百分比{get;set;}
公共双最大值{get;set;}
公共双最小值{get;set;}
公共bool highToLow{get;set;}
公共长生成{get;set;}
}
公共类根对象
{
公共购买{get;set;}
公开出售{get;set;}
}
静态void Main(字符串[]参数)
{
字符串sURL=”https://api.evemarketer.com/ec/marketstat/json?typeid=1230®ionlimit=10000002";
StreamReader objReader=新的StreamReader(WebRequest.Create(sURL.GetResponse().GetResponseStream());
字符串sLine=objReader.ReadLine();
var obj=JToken.Parse(sLine.ToObject();
Console.WriteLine(“购买节点:”);
Console.WriteLine(“Max:+obj[0].buy.Max”);
Console.WriteLine(“HighToLow:+obj[0].buy.HighToLow);
Console.WriteLine(“销售节点:”);
Console.WriteLine(“Max:+obj[0].sell.Max”);
Console.WriteLine(“HighToLow:+obj[0].sell.HighToLow);
Console.ReadLine();
}
}
}
为什么您的所有属性都是静态的?尝试从属性中删除静态。为什么要尝试删除JSON片段。不要那样做。这是一个有效的JSON。将流读到底,并将其反序列化为更简单的类结构。您的Buy
和Sell
类是相同的,ForQuery
类也是相同的。每种类型只能有一种。您可以使用属性名称(买卖)来区分这两个对象。仅此而已。此外,如果需要更改属性的名称,可以使用属性[JsonProperty(“[OriginalPropertyName]”)
。我使用静态,因为我的代码的设置方式会抛出一个错误,说它不能使用非静态值。
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();
}
}
}