Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试反序列化JSON并在DataGridView中显示结果时遇到异常_C#_Json_Datagridview_Json.net - Fatal编程技术网

C# 尝试反序列化JSON并在DataGridView中显示结果时遇到异常

C# 尝试反序列化JSON并在DataGridView中显示结果时遇到异常,c#,json,datagridview,json.net,C#,Json,Datagridview,Json.net,我有一些C代码,从API中获取JSON数据。JSON如下所示: { "count": 32696, "results": [{ "data_id": 0, "name": "Extended Potion of Ghost Slaying", "rarity": 0, "restriction_level": 0, "img": "", "type_id": 0, "s

我有一些C代码,从API中获取JSON数据。JSON如下所示:

{
    "count": 32696,
    "results": [{
        "data_id": 0,
        "name": "Extended Potion of Ghost Slaying",
        "rarity": 0,
        "restriction_level": 0,
        "img": "",
        "type_id": 0,
        "sub_type_id": 0,
        "price_last_changed": "2013-03-18 17:00:31 UTC",
        "max_offer_unit_price": 0,
        "min_sale_unit_price": 0,
        "offer_availability": 0,
        "sale_availability": 0,
        "sale_price_change_last_hour": 0,
        "offer_price_change_last_hour": 0
    }]
}
internal class MyClass
{
    public int data_id { get; set; }
    public string name { get; set; }
    public int rarity { get; set; }
    public int restriction_level { get; set; }
    public string img { get; set; }
    public int type_id { get; set; }
    public int sub_type_id { get; set; }
    public string price_last_changed { get; set; }
    public int max_offer_unit_price { get; set; }
    public int min_sale_unit_price { get; set; }
    public int offer_availability { get; set; }
    public int sale_availability { get; set; }
    public int sale_price_change_last_hour { get; set; }
    public int offer_price_change_last_hour { get; set; }
}

internal class RootObject
{
    public int count { get; set; }
    public List<MyClass> results { get; set; }
}
(但结果中不止一项。)

我做了两个这样的课程:

{
    "count": 32696,
    "results": [{
        "data_id": 0,
        "name": "Extended Potion of Ghost Slaying",
        "rarity": 0,
        "restriction_level": 0,
        "img": "",
        "type_id": 0,
        "sub_type_id": 0,
        "price_last_changed": "2013-03-18 17:00:31 UTC",
        "max_offer_unit_price": 0,
        "min_sale_unit_price": 0,
        "offer_availability": 0,
        "sale_availability": 0,
        "sale_price_change_last_hour": 0,
        "offer_price_change_last_hour": 0
    }]
}
internal class MyClass
{
    public int data_id { get; set; }
    public string name { get; set; }
    public int rarity { get; set; }
    public int restriction_level { get; set; }
    public string img { get; set; }
    public int type_id { get; set; }
    public int sub_type_id { get; set; }
    public string price_last_changed { get; set; }
    public int max_offer_unit_price { get; set; }
    public int min_sale_unit_price { get; set; }
    public int offer_availability { get; set; }
    public int sale_availability { get; set; }
    public int sale_price_change_last_hour { get; set; }
    public int offer_price_change_last_hour { get; set; }
}

internal class RootObject
{
    public int count { get; set; }
    public List<MyClass> results { get; set; }
}
内部类MyClass
{
公共int数据_id{get;set;}
公共字符串名称{get;set;}
公共整数稀有性{get;set;}
公共整数限制_级别{get;set;}
公共字符串img{get;set;}
公共int类型_id{get;set;}
public int sub_type_id{get;set;}
公共字符串price\u last\u changed{get;set;}
公共整数最大报价单位价格{get;set;}
公共内部最小销售单位价格{get;set;}
公共int提供可用性{get;set;}
public int sale_可用性{get;set;}
公开国际销售价格变动最后一小时{get;set;}
公共int提供(价格)(变动)(最后)小时{get;set;}
}
内部类根对象
{
公共整数计数{get;set;}
公共列表结果{get;set;}
}
下面是我获取JSON并将其反序列化的部分:

using (WebClient wc = new WebClient())
{
    string URI = "a good url";

    wc.Headers.Add("Content-Type", "text");
    string HtmlResult = wc.DownloadString(URI);
    MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(HtmlResult);
    DataTable dt = (DataTable)JsonConvert.DeserializeObject(HtmlResult, (typeof(DataTable)));
    this.dataGridView1.DataSource = dt;
}
使用(WebClient wc=new WebClient())
{
string URI=“一个好的url”;
添加(“内容类型”、“文本”);
字符串HtmlResult=wc.DownloadString(URI);
MyClass[]结果=JsonConvert.DeserializeObject(HtmlResult);
DataTable dt=(DataTable)JsonConvert.DeserializeObject(HtmlResult,(typeof(DataTable));
this.dataGridView1.DataSource=dt;
}
但当我运行此代码时,我得到一个错误:

其他信息:无法反序列化当前JSON对象 (例如,{“name”:“value”})转换为类型“gwspiderv2.MyClass[]”,因为 类型需要JSON数组(例如[1,2,3])才能正确反序列化


我已经在另一个API上使用了这种类型的代码,没有错误。我做错了什么?

使用RootObject进行反序列化,如下所示,它有MyClass的列表

RootObject result = JsonConvert.DeserializeObject<RootObject>(HtmlResult);
RootObject result=JsonConvert.DeserializeObject(HtmlResult);

使用RootObject进行反序列化,如下所示,它有MyClass的列表

RootObject result = JsonConvert.DeserializeObject<RootObject>(HtmlResult);
RootObject result=JsonConvert.DeserializeObject(HtmlResult);

在您的代码中,您似乎试图以两种不同的方式反序列化相同的JSON,这没有什么意义:

MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(HtmlResult);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(HtmlResult, (typeof(DataTable)));

在您的代码中,您似乎试图以两种不同的方式反序列化相同的JSON,这没有多大意义:

MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(HtmlResult);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(HtmlResult, (typeof(DataTable)));

是的,有一(1)个对象的JSON,它本身,而不是在数组中,只是带着大括号坐在那里。它在方括号内吗?不,不是。在JSON中,数组是以逗号分隔的方括号之间的对象或值序列。您告诉反序列化程序反序列化一个数组--您告诉它坚持使用一个数组。所以它寻找一个数组。相反,它会找到你给它的东西。所以它说“这不是一个数组”。您的原始JSON是什么样子的?根据错误,它必须是JSON对象而不是数组。也许你想将JSON反序列化为你不使用的
RootObject
类型?JSON看起来像:{“count”:32696,“results”:[{“data_id”:0,“name::“Extended Potion of Ghost Slaying”,“rarity”:0,“restriction_level”:0,“img:”,“type_id”:0,“sub_type_id”:0,“price_last_changed”:“2013-03-18 17:00:31 UTC”,“max_offer_unit_price”:0,“min_sale_unit_price”:0,“offer_availability”:0,“sale_availability”:0,“sale_price_change_last_hour”:0,“offer_price_change_last_hour”:0},{“data_id”:1,“name”:“MONSTER ONLY Moa Unarmed*,诸如此类,你有一(1)个JSON对象本身,不在数组中,只是带着大括号坐在那里。它在方括号之间吗?不。它不是。在JSON中,数组是方括号之间以逗号分隔的对象或值序列。你告诉反序列化程序反序列化数组——你告诉它坚持数组。因此它寻找数组。instad,它会找到你给它的东西。所以它说“这不是一个数组”。你的原始JSON看起来像什么?根据错误,它必须是JSON对象而不是数组。也许你是想将JSON反序列化为你不使用的
RootObject
类型?JSON看起来像:{“count”:32696,“results”:[{“data\u id”:0,“name”:“鬼魂杀戮扩展药剂”,“稀有”:0,“限制等级”:0,“img”:“类型id”:0,“子类型id”:0,“价格上次变化”:“2013-03-18 17:00:31 UTC”,“最高报价单价”:0,“最低销售单价”:0,“报价有效性”:0,“销售有效性”:0,“价格上次变化价格上次变化”:0,“报价上次变化价格上次变化价格”:0,“数据id”:1,“名称”:MONSTER ONLY Moa Unarmed*等此错误:附加信息:读取数据表时意外的JSON标记。应为StartAry,获取StartObject.Path“”,第1行,位置1。获取此错误:附加信息:读取数据表时意外的JSON标记。应为StartAry,获取StartObject.Path“”,第1行,位置1。