locked
Deserialized dataset to object=emtpy values RRS feed

  • Question

  • User1021841275 posted

    Car.CarProdCost[] CarCost ;

    MyData.GetCarProdCost returns a dataset;

    System.Data.DataSet results = await MyData.GetCarProdCost("123456");
    string strToConvert = JsonConvert.SerializeObject(results.Tables[0]);
    CarCost = JsonConvert.DeserializeObject<Car.CarProdCost[]>(strToConvert);

    The object CarCost returns 2 objects/rows: The values if I try to retrieve for example CarCost.ModelYear are always empty. If I iterate the dataRows in the table with a for statement, all the values are there. Any ideas on what I'm doing wrong?

    Tuesday, October 1, 2019 1:39 PM

Answers

  • User753101303 posted

    Hi,

    This is an array of CarProdCost or of CarCost. Check the serialized Json string maybe wirth a single test row and double check you are the same structure on the other side.

    I tried the following which works fine :

       class Data
            {
                public string A { get; set; }
                public string B { get; set; }
            }
            static async Task MainAsync()
            {
                var dt = new DataTable();
                dt.Columns.Add("A");
                dt.Columns.Add("B");
                dt.Rows.Add(new string[] { "a", "b" });
                dt.Rows.Add(new string[] { "c", "d" });
                var str=(Newtonsoft.Json.JsonConvert.SerializeObject(dt));
                var a = Newtonsoft.Json.JsonConvert.DeserializeObject<Data[]>(str);
                foreach(var item in a)
                {
                    Console.WriteLine("{0}, {1}", item.A, item.B);
                }
    }

    shows :

    a, b
    c, d

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 1, 2019 11:39 PM
  • User665608656 posted

    Hi blzr,

    The object CarCost returns 2 objects/rows: The values if I try to retrieve for example CarCost.ModelYear are always empty. If I iterate the dataRows in the table with a for statement, all the values are there. Any ideas on what I'm doing wrong?

    The CarCost in your code is an array , no matter how it fills in values, if you want to get specific values in array in C#, you have to get them through loops.

    Or if you know which data you will get, you can get the specific value of the array directly through index, for example:

    string a = CarCost[0].ModelYear.ToString();

    If you do like this, you can get the first row data of ModelYear.

    If you want to get all the data, you need to go through the loop, as follows:

     foreach (var item in CarCost)
     {
          string a = item.ModelYear.ToString();
    // do something you want }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 2:59 AM

All replies

  • User303363814 posted

    What is in strToConvert?  (Show us the actual value from the debugger)

    What is the definition of CarProdCost? (Show your code)

    Tuesday, October 1, 2019 10:07 PM
  • User753101303 posted

    Hi,

    This is an array of CarProdCost or of CarCost. Check the serialized Json string maybe wirth a single test row and double check you are the same structure on the other side.

    I tried the following which works fine :

       class Data
            {
                public string A { get; set; }
                public string B { get; set; }
            }
            static async Task MainAsync()
            {
                var dt = new DataTable();
                dt.Columns.Add("A");
                dt.Columns.Add("B");
                dt.Rows.Add(new string[] { "a", "b" });
                dt.Rows.Add(new string[] { "c", "d" });
                var str=(Newtonsoft.Json.JsonConvert.SerializeObject(dt));
                var a = Newtonsoft.Json.JsonConvert.DeserializeObject<Data[]>(str);
                foreach(var item in a)
                {
                    Console.WriteLine("{0}, {1}", item.A, item.B);
                }
    }

    shows :

    a, b
    c, d

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 1, 2019 11:39 PM
  • User665608656 posted

    Hi blzr,

    The object CarCost returns 2 objects/rows: The values if I try to retrieve for example CarCost.ModelYear are always empty. If I iterate the dataRows in the table with a for statement, all the values are there. Any ideas on what I'm doing wrong?

    The CarCost in your code is an array , no matter how it fills in values, if you want to get specific values in array in C#, you have to get them through loops.

    Or if you know which data you will get, you can get the specific value of the array directly through index, for example:

    string a = CarCost[0].ModelYear.ToString();

    If you do like this, you can get the first row data of ModelYear.

    If you want to get all the data, you need to go through the loop, as follows:

     foreach (var item in CarCost)
     {
          string a = item.ModelYear.ToString();
    // do something you want }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 2:59 AM