3

I get all JSON data to my string, but I wanna get item by item, because I need to add my items to database..

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

My output:

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
}

please help me

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
devx
  • 37
  • 5
  • Have a look on [Json.Net Resolving Property Names](http://stackoverflow.com/a/32621752/3796048) you need to modify your class accordingly. – Mohit S Sep 29 '15 at 07:11
  • how can i print my results?i try this: ia.Rows[cou].GetType().GetProperties().ToList().ForEach(b => { Name = b.Name.ToString(); Console.WriteLine(b+" "+ia.Rows[cou]); }); how can i get value here Console.WriteLine(b+" "+ia.Rows[cou].HERE I WANNA ADD NAME VARIABLE); – devx Sep 29 '15 at 08:57
  • Hi, i try to add new item,
    RootObject newRow = new RootObject(); newRow.Rows[0].Name="sdaad"; but i get error: Additional information: Object reference not set to an instance of an object. in this line: newRow.Rows[0].Name="sdaad"; Can anyone know what is wrong?
    – devx Sep 30 '15 at 11:44
  • no one? please i realy need this.. – devx Oct 01 '15 at 05:21
  • 1
    I have answered. have a look. – Mohit S Oct 01 '15 at 06:48

3 Answers3

1

From your string res JSON the C# class structure should be

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

Now deserialize

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

i.Rows will have the list of items you needed. Loop through each of the item to save it to data base or perform any operation you want.

Class output generated by : Json2Csharp

John
  • 351
  • 4
  • 16
  • it works thx.. can i ask you how print my results i try: ia.Rows[cou].GetType().GetProperties().ToList().ForEach(b => { Name = b.Name.ToString(); Console.WriteLine(b+" "+ia.Rows[cou]); }); how can i get value here Console.WriteLine(b+" "+ia.Rows[cou].HERE I WANNA ADD NAME VARIABLE); – devx Sep 29 '15 at 08:24
  • I am not sure which Name variable you want to add there... Can you please provide more details? – John Sep 29 '15 at 09:06
  • this variable Name = b.Name.ToString(); – devx Sep 29 '15 at 09:47
  • do you know how can i read all result? beacause i can read only 10 results from first page. – devx Sep 30 '15 at 05:45
  • And do you any body know how add my result to datagridview – devx Sep 30 '15 at 06:42
  • @devx I am busy at work so will able to help you out during weekends. – John Oct 02 '15 at 08:25
  • Do you want to read all the items in `List` ? If yes then use a for each loop. `foreach(Row item in RootObject.Rows ) { Console.WriteLine("ItemId = " + item.ItemId);Console.WriteLine("Title = " + item.Title ); /*You will get all the properties of Row class here.*/}` If you want to use for loop then. `for(int i=0;i – John Oct 05 '15 at 08:23
  • i need to get 100 rows(all) but i get only 10 rows(only data on first page). – devx Oct 05 '15 at 12:52
0

You are trying to deserialize an object that has collection of Rows and a couple of other meta fields into an instance of Item, that obviously does not has same fields.

You have to implement just one more class, let's call it 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;} 
}

And deserialize your data into it. var data = JsonConvert.DeserializeObject<ItemsPage>(res); After that, you will have collection of 10 of Item inside your data.

Also, as I can see, that data is not full, since you api uses paging (Page=1, TotalRows = 100, PageSize = 10) and you will have to get other pages after that.

EDIT: updated PageSize from 20 to 10 since I read the question wrong somehow.

I have pagesize 20 in my projects

maiksaray
  • 358
  • 2
  • 14
  • Thx. Now how can i get all items? Now i can read only 10 items for (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 Sep 29 '15 at 10:30
  • @devx First of all, you are using for-cycle that counts to 10, replace 10 with 20 and get 20 items. Secondly, there is foreach cycle `foreach(Item item in ia.Rows){} – maiksaray Sep 29 '15 at 12:22
  • Its cout to 10, because if i give more ten 10 i get error.. can you please tell me how to get items with foreach? – devx Sep 29 '15 at 12:33
  • please can you help me? – devx Sep 30 '15 at 05:02
  • @devx yes, I can, but that's different topic. You can look [here](https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx) for foreach usage. – maiksaray Sep 30 '15 at 07:51
  • @devx sorry for misleading you. you do really have only 10 items it your collection and will get error if you try to get 11th item. To get other items you should make another API call for second page of items in JSON. Since we do not know what that API might be, can't help you more – maiksaray Sep 30 '15 at 07:55
  • @devx hmm, there are lots of questions like that on SO. for example [this](http://stackoverflow.com/questions/26491961/how-to-bind-a-collection-of-objects-to-a-datagridview-in-winforms) – maiksaray Sep 30 '15 at 08:10
0

I believe this will do the trick for you. You can also use Visual Studio's Paste Special option :)

Serialization and Deserialization

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

Class Structure

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; }
    }
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69