【问题标题】:How to replace multiple values in json file using c#?如何使用c#替换json文件中的多个值?
【发布时间】:2020-02-04 10:51:00
【问题描述】:

如果 API 编号存在于 json 中,我需要用新值替换 json 文件中的文件名和位置,否则它应该使用 API、文件和位置添加新的 json 数组。我已经编写了 foreach 循环来执行此操作,但每次添加内容以列出 if 条件将添加的新 api 并与列表进行比较,因此它不断将相同的 api 一次又一次地添加到 json 文件中。请帮我解决这个问题..

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 文件如下:

[
  {
    "API": "422833682700000000",
    "File": "daf420.dat.07-31-2019",
    "Location": 2922
  },
  {
    "API": "422833682700000000",
    "File": "daf420.dat.07-31-2019",
    "Location": 2922
  }
]

【问题讨论】:

标签: c# json list serialization json-deserialization


【解决方案1】:

这是一个代码,如果具有此类“API”的项目不存在,它将将该项目添加到dpIndexList,或者如果具有此类“API”的项目存在,则将更新该列表中的项目:

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

为了在本地进行测试,我使用了以下虚拟列表,并且效果很好:

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

附:不要忘记在文件顶部添加using System.Linq;

【讨论】:

  • @AnjanaJain ,我已经更新了代码,你能再检查一下吗?
  • @AnjanaJain,太好了。请将其标记为答案,以便其他人找到它。
【解决方案2】:

查看您的代码后。看起来您正在获取值形式 同一个对象并一遍又一遍地改变它 看下面的例子

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

如您所见,我在 List dpIndexList 中添加了索引对象 但是在添加它之后,我在同一个索引对象中更改了 API 的值,这也导致了 dpIndex 列表中的值也发生了变化。 你在这里对 item 做同样的事情,你在每次迭代中改变它的状态。所以最终所有的值都将成为循环的最终迭代 我说您在每次迭代中创建一个对象并将其添加到列表中 对于更新使用 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);
    }
}

我相信这会对你有所帮助

【讨论】:

  • 是的,它有帮助..谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
相关资源
最近更新 更多