获取多个JSON数据

时间:2015-09-29 06:55:22

标签: c# json

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

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

CLASS:Item

namespace Api
{
    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 RevenueAccountEU
    {
        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 Item
    {
        //public int ItemId { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        //public string EANCode { get; set; }
        //public string Description { get; set; }
        public string ItemType { get; set; }
        public string UnitOfMeasurement { get; set; }
        public VatRate VatRate { get; set; }
        public mMApiFkField VatRate { get; set; }
        public double Price { get; set; }
        public double RebatePercent { get; set; }
        public string Usage { get; set; }
        public Currency Currency { get; set; }
        public RevenueAccountDomestic RevenueAccountDomestic { get; set; }
        public RevenueAccountEU RevenueAccountEU { get; set; }
        public RevenueAccountOutsideEU RevenueAccountOutsideEU { get; set; }
        //public object StocksAccount { get; set; }
        //public string RecordDtModified { get; set; }
        //public string RowVersion { get; set; }*/
    }
}

我的输出:

string res=
{
    "Rows": [
        {
            "ItemId": 2683406,
            "Title": "Izdelava etiket",
            "Code": "#75603",
            "UnitOfMeasurement": "",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.46,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683407,
            "Title": "Fotografija 10 x1 3",
            "Code": "",
            "UnitOfMeasurement": "kos",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.22,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683408,
            "Title": "Digitalna fotografija 10 x 15",
            "Code": "",
            "UnitOfMeasurement": "kos",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.45,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683409,
            "Title": "Fotografija 9x13",
            "Code": "",
            "UnitOfMeasurement": "kos",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.2,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683410,
            "Title": "Digitalna fotografija 9 x 13",
            "Code": "",
            "UnitOfMeasurement": "kos",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.4,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683411,
            "Title": "Fotokopija A4",
            "Code": "",
            "UnitOfMeasurement": "kos",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0.1,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683442,
            "Title": "Snemanje oglasnih sporočil",
            "Code": "",
            "UnitOfMeasurement": "",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683443,
            "Title": "storitev",
            "Code": "",
            "UnitOfMeasurement": "",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 0,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683444,
            "Title": "POSLOVNO SVETOVANJE",
            "Code": "",
            "UnitOfMeasurement": "ura",
            "ItemType": "S",
            "VatRate": {
                "ID": 36,
                "Name": "S",
                "ResourceUrl": "/api/orgs/138657/vatrates/36"
            },
            "Price": 30,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        },
        {
            "ItemId": 2683445,
            "Title": "Obračun provizije po pogodbi za opravljene storitve zavarovalniškega zastopanja",
            "Code": "",
            "UnitOfMeasurement": null,
            "ItemType": "S",
            "VatRate": {
                "ID": 29,
                "Name": "O",
                "ResourceUrl": "/api/orgs/138657/vatrates/29"
            },
            "Price": 0,
            "Currency": {
                "ID": 7,
                "Name": "EUR",
                "ResourceUrl": "/api/orgs/138657/currencies/7"
            },
            "RevenueAccountDomestic": {
                "ID": 41636584,
                "Name": "Prihodki od prodaje storitev na domačem trgu",
                "ResourceUrl": "/api/orgs/138657/accounts/41636584"
            },
            "RevenueAccountOutsideEU": {
                "ID": 41636592,
                "Name": "Prihodki od prodaje storitev na trgu izven EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636592"
            },
            "RevenueAccountEU": {
                "ID": 41636591,
                "Name": "Prihodki od prodaje storitev na trgu EU",
                "ResourceUrl": "/api/orgs/138657/accounts/41636591"
            },
            "StocksAccount": null
        }
    ],
    "TotalRows": 100,
    "CurrentPageNumber": 1,
    "PageSize": 10
}
string json=
{
    "ItemId": 0,
    "Name": null,
    "Code": null,
    "ItemType": null,
    "UnitOfMeasurement": null,
    "VatRate": null,
    "Price": 0,
    "RebatePercent": 0,
    "Usage": null,
    "Currency": null,
    "RevenueAccountDomestic": null,
    "RevenueAccountEU": null,
    "RevenueAccountOutsideEU": null
}

请帮帮我

3 个答案:

答案 0 :(得分:1)

从你的string 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; }
}

现在反序列化

RootObject i = JsonConvert.DeserializeObject<RootObject>(res);

i.Rows将包含您需要的项目列表。遍历每个项目以将其保存到数据库或执行您想要的任何操作。

Json2Csharp

生成的类输出

答案 1 :(得分:0)

您正尝试将具有Rows集合和其他几个元字段的对象反序列化为Item的实例,这显然没有相同的字段。

你必须再实施一个课程,我们称之为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;} 
}

将数据反序列化到其中。 var data = JsonConvert.DeserializeObject<ItemsPage>(res);之后,您的数据中将收集10个Item

另外,正如我所看到的那样,数据未满,因为你api使用分页(Page = 1,TotalRows = 100,PageSize = 10),之后你必须得到其他页面。

编辑:更新PageSize从20到10,因为我以某种方式错误地阅读了这个问题。

我的项目中的页面大小为

答案 2 :(得分:0)

我相信这会为你做到这一点。您还可以使用Visual Studio的“选择性粘贴”选项:)

序列化和反序列化

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; }
    }
}