Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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数据_C#_Json - Fatal编程技术网

C# 获取多个JSON数据

C# 获取多个JSON数据,c#,json,C#,Json,我将所有JSON数据都添加到字符串中,但我希望逐项获取,因为我需要将我的项添加到数据库中 static void Main() { string res = getItems.getItemsApi(); Item i = JsonConvert.DeserializeObject<Item>(res); string json = JsonConvert.SerializeObject(i); } 我的输出: 请从您的字符串resJSON中帮助我,C#类

我将所有JSON数据都添加到字符串中,但我希望逐项获取,因为我需要将我的项添加到数据库中

static void Main()
{
    string res = getItems.getItemsApi();
    Item i = JsonConvert.DeserializeObject<Item>(res);
    string json = JsonConvert.SerializeObject(i);
}
我的输出:

请从您的
字符串res
JSON中帮助我,C#类结构应该是

public class VatRate
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class Currency
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountDomestic
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountOutsideEU
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountEU
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class Row
{
    public int ItemId { get; set; }
    public string Title { get; set; }
    public string Code { get; set; }
    public string UnitOfMeasurement { get; set; }
    public string ItemType { get; set; }
    public VatRate VatRate { get; set; }
    public double Price { get; set; }
    public Currency Currency { get; set; }
    public RevenueAccountDomestic RevenueAccountDomestic { get; set; }
    public RevenueAccountOutsideEU RevenueAccountOutsideEU { get; set; }
    public RevenueAccountEU RevenueAccountEU { get; set; }
    public object StocksAccount { get; set; }
}

public class RootObject
{
    public List<Row> Rows { get; set; }
    public int TotalRows { get; set; }
    public int CurrentPageNumber { get; set; }
    public int PageSize { get; set; }
}
公共类增值税
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串ResourceUrl{get;set;}
}
公营货币
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串ResourceUrl{get;set;}
}
国内公共类收入
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串ResourceUrl{get;set;}
}
欧盟以外国家的公共类收入
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串ResourceUrl{get;set;}
}
公共类收入国家
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串ResourceUrl{get;set;}
}
公共类行
{
公共int ItemId{get;set;}
公共字符串标题{get;set;}
公共字符串代码{get;set;}
公共字符串度量单位{get;set;}
公共字符串ItemType{get;set;}
公共增值税增值税{get;set;}
公共双价{get;set;}
公共货币{get;set;}
公共收入国家国内收入国家国内{get;set;}
公共收入超出欧盟收入超出欧盟{get;set;}
public RevenueAccountEU RevenueAccountEU{get;set;}
公共对象StocksAccount{get;set;}
}
公共类根对象
{
公共列表行{get;set;}
公共整数TotalRows{get;set;}
public int CurrentPageNumber{get;set;}
公共int PageSize{get;set;}
}
现在反序列化

RootObject i = JsonConvert.DeserializeObject<RootObject>(res);
RootObject i=JsonConvert.DeserializeObject(res);
i.Rows
将提供您所需的项目列表。循环遍历每个项以将其保存到数据库或执行任何您想要的操作


生成的类输出:

您试图将一个包含
行集合的对象和两个其他元字段反序列化为
项的实例,该实例显然没有相同的字段

您只需再实现一个类,我们称之为
ItemsPage

public class ItemsPage
{
    public List<Item> Rows {get; set;}
    public int TotalRows {get; set;}
    public int CurrentPageNnumber {get; set;}
    public int PageSize {get; set;} 
}
公共类ItemsPage
{
公共列表行{get;set;}
公共整数TotalRows{get;set;}
public int CurrentPageNnumber{get;set;}
公共int PageSize{get;set;}
}
并将数据反序列化到其中
var data=JsonConvert.DeserializeObject(res)之后,您将在数据中收集10个

另外,正如我所看到的,该数据不是完整的,因为您的api使用分页(Page=1,TotalRows=100,PageSize=10),然后您将不得不获取其他页面

编辑:
PageSize
从20更新到10,因为我不知怎么看错了问题


我的项目中有页面大小为20的页面

我相信这会为您带来好处。您还可以使用Visual Studio的“粘贴”特殊选项:)

序列化和反序列化
查看一下您需要相应地修改您的类。如何打印结果?我尝试以下操作:ia.Rows[cou].GetType().GetProperties().ToList().ForEach(b=>{Name=b.Name.ToString();Console.WriteLine(b+“”+ia.Rows[cou]);});如何在Console.WriteLine(b+“”+ia.Rows[cou]。这里我想添加名称变量)中获取值;您好,我尝试添加新项,
RootObject newRow=newrootobject();newRow.Rows[0].Name=“sdaad”;但我得到的错误是:附加信息:对象引用未设置为对象的实例。此行中:newRow.Rows[0].Name=“sdaad”;有人知道怎么回事吗?没人知道吗?求你了,我真的需要这个……我已经回答了。看一看,它能起作用。。我可以问你我如何打印我尝试的结果吗:ia.Rows[cou].GetType().GetProperties().ToList().ForEach(b=>{Name=b.Name.ToString();Console.WriteLine(b+“”+ia.Rows[cou]);});如何在Console.WriteLine(b+“”+ia.Rows[cou]。这里我想添加名称变量)中获取值;我不确定您要在那里添加哪个名称变量。。。能否提供更多详细信息?此变量名=b.Name.ToString();你知道我怎样才能阅读所有的结果吗?因为我只能从第一页读取10个结果。你们知道如何将我的结果添加到datagridviewThx吗。现在我怎样才能得到所有物品?现在我只能为(int cou=0;cou<10;cou++){ia.Rows[cou].GetType().GetProperties().ToList().ForEach(b=>{Name=b.Name.ToString();Console.WriteLine(b+“”+ia.Rows[cou]);}@devx读取10项首先,使用计数为10的for循环,将10替换为20,得到20项。其次,有一个foreach循环`foreach(ia.Rows中的Item){}它的cout到10,因为如果我给10多个10,我会得到错误。。你能告诉我如何使用foreach获取物品吗?你能帮我吗?@devx是的,我能,但那是不同的主题。您可以查找foreach用法。
public class VatRate
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class Currency
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountDomestic
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountOutsideEU
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class RevenueAccountEU
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ResourceUrl { get; set; }
}

public class Row
{
    public int ItemId { get; set; }
    public string Title { get; set; }
    public string Code { get; set; }
    public string UnitOfMeasurement { get; set; }
    public string ItemType { get; set; }
    public VatRate VatRate { get; set; }
    public double Price { get; set; }
    public Currency Currency { get; set; }
    public RevenueAccountDomestic RevenueAccountDomestic { get; set; }
    public RevenueAccountOutsideEU RevenueAccountOutsideEU { get; set; }
    public RevenueAccountEU RevenueAccountEU { get; set; }
    public object StocksAccount { get; set; }
}

public class RootObject
{
    public List<Row> Rows { get; set; }
    public int TotalRows { get; set; }
    public int CurrentPageNumber { get; set; }
    public int PageSize { get; set; }
}
RootObject i = JsonConvert.DeserializeObject<RootObject>(res);
public class ItemsPage
{
    public List<Item> Rows {get; set;}
    public int TotalRows {get; set;}
    public int CurrentPageNnumber {get; set;}
    public int PageSize {get; set;} 
}
static void Main()
{
    string res = getItems.getItemsApi();
    RootElement i = JsonConvert.DeserializeObject<RootElement>(res);
    string json = JsonConvert.SerializeObject(i);
}
namespace Api
{
    public class RootElement
    {
        [JsonProperty("Rows")]
        public Item Items { get; set; }
    }
    public class Currency
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("ResourceUrl")]
        public string ResourceUrl { get; set; }
    }
    public class RevenueAccountDomestic
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("ResourceUrl")]
        public string ResourceUrl { get; set; }
    }
    public class RevenueAccountEU
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("ResourceUrl")]
        public string ResourceUrl { get; set; }    
    }
    public class RevenueAccountOutsideEU
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("ResourceUrl")]
        public string ResourceUrl { get; set; }
    }
    public class Item
    {
        [JsonProperty("ItemId")]
        public int ItemId { get; set; }

        [JsonProperty("Title")]
        public string Name { get; set; }

        [JsonProperty("Code")]
        public string Code { get; set; }

        [JsonProperty("UnitOfMeasurement")]
        public string UnitOfMeasurement { get; set; }

        [JsonProperty("ItemType")]
        public string ItemType { get; set; }

        [JsonProperty("VatRate")]
        public VatRate VatRate { get; set; }

        [JsonProperty("Price")]
        public double Price { get; set; }

        [JsonProperty("Currency")]
        public Currency Currency { get; set; }

        [JsonProperty("RevenueAccountDomestic")]
        public RevenueAccountDomestic RevenueAccountDomestic { get; set; }

        [JsonProperty("RevenueAccountOutsideEU")]
        public RevenueAccountOutsideEU RevenueAccountOutsideEU { get; set; }

        [JsonProperty("RevenueAccountEU")]
        public RevenueAccountEU RevenueAccountEU { get; set; }

        [JsonProperty("StocksAccount")]
        public object StocksAccount { get; set; }

        [JsonProperty("TotalRows")]
        public int TotalRows { get; set; }

        [JsonProperty("CurrentPageNumber")]
        public int CurrentPageNumber { get; set; }

        [JsonProperty("PageSize")]
        public int PageSize { get; set; }
    }
}