0

I am creating a dynamic Jobject from a data set table. The object is expected to have set of 3 key/value pair.

{
    "Lowest": "12.50",
    "Highest": "",
    "Type": "normal"
}

I am looping through a table to generate the array of the object code

dynamic product_Price = new JObject();
JArray product_Price_array = new JArray();

for (int i = 0; i <= ds.Tables[0].Rows.Count-1; i++)
{
   product_Price.RemoveAll();
   // this is loop through columns to generate the dynamic key value  pair of 
   the object 
   for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
   {


   product_Price.Add(ds.Tables[0].Columns[j].ColumnName,ds.Tables[0].Rows[i] 
   [j].ToString());


    }
 // now that i have object end of the loop i am adding to the array. but here its always duplicate of object not unique                     
 product_Price_array.Add(product_Price);
}

Current Result : Duplicate jobject is added and its always the last entry from the ds.Tables[0] loop

[
{
    "Lowest": "17.50",
    "Highest": "",
    "Type": "kid"
},

{
    "Lowest": "17.50",
    "Highest": "",
    "Type": "kid"
}
]

I have already tried using list , static object instead of dynamic object.

Expected result

[
{
    "Lowest": "12.50",
    "Highest": "",
    "Type": "normal"
},

{
    "Lowest": "17.50",
    "Highest": "",
    "Type": "kid"
}
]
```
  • 3
    because you are adding the same object – Selvin Apr 24 '19 at 08:24
  • 2
    you could make in every loop a new JObject for the product_Price instead of always removing the content. Just take the variable within the first for loop. – Leon Apr 24 '19 at 08:25
  • 1
    Possible duplicate of [Issue with List.Add() only saving the last added item](https://stackoverflow.com/questions/10644412/issue-with-list-add-only-saving-the-last-added-item) – Selvin Apr 24 '19 at 08:50
  • why it has +1 ... there is already bazillion similar question ... and answering to this one is just for "free rep farming" – Selvin Apr 24 '19 at 08:51
  • @Selvin, just close it as a dupe, then. – spodger Apr 24 '19 at 08:54

2 Answers2

0

As mentioned by Leon in comments you should use a new JObject in your loop instead of the array reference you have.

Reason why your code is adding the same values (objects really):

The memory location for your object gets allocated when you create it:

dynamic product_Price = new JObject();

When you add the object to the array, a reference of the address is stored

product_Price_array.Add(product_Price);

On clearing it, and then adding new values in product_Price, the values inside are removed, and new values are added, but the original location has not changed, so all the object you add to product_Price_array are pointing to the same address (and also the same object).

product_Price.RemoveAll();

If instead RemoveAll, you would just create a new JObject, this will create a new product_Price object at a different address, which will then get added to your array at the end.

product_Price = new JObject();

I would suggest you have a look at how arrays work

Also, not sure why are you using a Dynamic object why not just use a JObject

peeyush singh
  • 1,337
  • 1
  • 12
  • 23
0

You only have one instance of a JObject, product_Price. On your first trip round the outer loop you set the values of product_Price and add a reference to product_Price to product_Price_array.

On the second trip round the outer loop you clear the contents of product_Price, which clears the contents of the first element of product_Price_array, because it's the same object instance. Then you fill it with the second set of data, which also fills the first element of product_Price_array then you add a second reference to product_Price to your product_Price_array.

As @Selvin and @Leon said, just create a new JObject each time, e.g.

for (int i = 0; i <= ds.Tables[0].Rows.Count-1; i++)
{
   dynamic product_Price = new JObject();
.....
spodger
  • 1,668
  • 1
  • 12
  • 16