0

I need to replace the Filename and Location in json file with new value if the API number exists in json or else it should add new json array with API,File and location.I have written foreach loop to do it but each time the contents are added to list the if condition takes the new api added and compares to the list so it keeps adding the same api again and again to json file.Plz help me resolve this..

List<DPIndex> items = JsonConvert.DeserializeObject<List<DPIndex>>(json);

foreach (var item in items)
{
    foreach (var list in dpIndexList)
    {
        if (item.API == list.API)
        {

            item.File = list.File;
            item.Location = list.Location;
        }
        else
        {
            item.API = list.API;
            item.File = list.File;
            item.Location = list.Location;

        }
    }
    dpNewIndexList.Add(item);
}

string dpIdxObj = JsonConvert.SerializeObject(dpNewIndexList, Formatting.Indented);

Json file is as below:

[
  {
    "API": "422833682700000000",
    "File": "daf420.dat.07-31-2019",
    "Location": 2922
  },
  {
    "API": "422833682700000000",
    "File": "daf420.dat.07-31-2019",
    "Location": 2922
  }
]
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37

2 Answers2

0

After going through your code. It looks like you are getting values form same object and changing it over and over again Look at the example below

List<DPIndex> dpIndexList = new List<DPIndex>();
DPIndex index = new DPIndex() { 
    API = "API.1",
    File="API1",
    Location="1"
};
dpIndexList.Add(index);
index.API = "API.2";

As you can see I have added the index object in List dpIndexList but after adding it I changed the value of API in same index object which leads to changed value in dpIndex List also. You are doing the same thing with item here you are changing its state in each iteration. So in the end all values will become what was the final iteration of the loop I say you create a object in each iteration and add it to list And for updation use lambda

foreach (var item in items)
{
    foreach (var list in dpIndexList)
    {
        DPIndex it = new DPIndex();
        if (item.API == list.API)
        {
            it.File = list.File;
            it.Location = list.Location;
            dpNewIndexList.RemoveAll(x=> x.API == list.API);
        }
        else
        {
            it.API = list.API;
            it.File = list.File;
            it.Location = list.Location;
        }
        dpNewIndexList.Add(it);
    }
}

I beleive this would help you

0

Here is a code which will add the item to dpIndexList if an item with such "API" doesn't exist there, or will update the item in that list, if an item with such "API" exists:

foreach (var item in items)
{
    // Check if the item with such API already exists in dpIndexList
    var foundItem = dpIndexList.FirstOrDefault(dpItem => dpItem.API == item.API);

    if (foundItem != null)
    {
        // Exists. Update the item in dpIndexList
        foundItem.File = item.File;
        foundItem.Location = item.Location;
    }
    else
    {
        // Doesn't exist. Adding to dpIndexList
        dpIndexList.Add(item);
    }
}

For testing locally I used the following dummy lists, and it worked:

var dpIndexList = new List<DPIndex>()
{
    new DPIndex
    {
        API = "1",
        File = "File_1_ORIG",
        Location = 1111
    },
    new DPIndex
    {
        API = "2",
        File = "File_2_ORIG",
        Location = 2222
    },
};

var items = new List<DPIndex>()
{
    // Item, which exists in dpIndexList (should update the original)
    new DPIndex
    {
        API = "2",
        File = "File_2_UPDATE",
        Location = 3333
    },
    // New item, which doesn't exist in dpIndexList (should be added)
    new DPIndex
    {
        API = "3",
        File = "File_3_NEW",
        Location = 3333,
    },
    // Item, which should UPDATE the one above (should update that one)
    new DPIndex
    {
        API = "3",
        File = "File_3_UPDATED",
        Location = 3333
    },
};

P.S. Don't forget to add using System.Linq; to the top of the file.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75