C# 编辑动态字符串

标签 c# json string

我有一个问题摆在我面前,我什至不确定如何处理它。

背景是我在 JSON 字符串中有一条来自 API 的“记录”。这个字符串是非常动态的,预测配对键几乎是不可能的。但是在记录中总是有四个字段,这就是我要更改的四个。

为了更新记录,必须将整个数据集与您希望更新的值一起发回(仅发回我要更新的字段会导致所有当前设置的字段被清除)

所以我的计划是,提取 JSON 字符串,将其保留为字符串格式,然后解析它以找到我想要更改的对,然后将 JSON 字符串返回给 API 和魔法!字段不会被删除。

所以我面临的问题是想出一个想法如何解析这个字符串,找到一个键值“keyvalue1”,然后更新它的值“keyvalue1”:“更新我”

这是我正在使用的字符串示例:(要更新的值 1 - 4 和需要更改的区域)

"{\"record\":
  {\"status\":\"241\",
   \"id\":\"a0de27a2-1447-4941-a3c0-8853e5682e85\",
   \"created_by\":\"The Amazing Mongo\",
   \"project_id\":null,
   \"changeset_id\":\"eeba5ba9-8305-4cb3-ad63-6d1bbff6b626\",
   \"valuetobeupdated1\":\"some long value needs to go in here\",
   \"valuetobeupdated2\":\"more data is needed here\",
   \"form_values\":      
    {\"833b\":\"00000\",
     \"683b\":\"00000\",
     \"62b2\":\"370\",
     \"6472\":\"615\",
     \"e4fa\":\"552\",
     \"7868\":\"1\",
     \"0d48\":\"4\",
     \"1d54\":\"25\",
     \"2155\":\"200\",
     \"6435\":\"2\",
     \"f4ad\":\"33\",
     \"6c2b\":\"108\",
     \"adb5\":\"62\",
     \"e622\":\"0\",
     \"d1f0\":\"25\",
     \"8cf6\":\"0\",
     \"80ad\":\"0\",
     \"6fe4\":\"0\",
     \"a148\":\"2016-05-13\",
     \"6f55\":\"11:49\",
     \"3b7c\":{\"choice_values\":[\"2409\"],
               \"other_values\":[]},
     \"valuetobeupdated3\":{\"choice_values\":[\"More information goes in here\"],
                            \"other_values\":[]
                            },
     \"valuetobeupdated4\":{\"choice_values\":[\"more information here\"],
                            \"other_values\":[]
                           },
     \"course\":null,}}"

所以我的问题是我如何着手开始这个,我从来都不擅长正则表达式,甚至不确定正则表达式是否可以做我想在这里做的事情。 “一些长值需要放在这里”和其他值可以是任意长度。

任何建议将不胜感激。

最佳答案

使用 Json.NET动态:

var root = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
root.record.valuetobeupdated1 = "Loonnnnnnnnnnnnnnnnnng value";
root.record.valuetobeupdated2 = "More data";
root.record.form_values.Remove("valuetobeupdated3");
root.record.form_values.Remove("valuetobeupdated4");

var output = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(output);

输出:

{
  "record": {
    "status": "241",
    "id": "a0de27a2-1447-4941-a3c0-8853e5682e85",
    "created_by": "The Amazing Mongo",
    "project_id": null,
    "changeset_id": "eeba5ba9-8305-4cb3-ad63-6d1bbff6b626",
    "valuetobeupdated1": "Loonnnnnnnnnnnnnnnnnng value",
    "valuetobeupdated2": "More data",
    "form_values": {
      "833b": "00000",
      "683b": "00000",
      "62b2": "370",
      "6472": "615",
      "e4fa": "552",
      "7868": "1",
      "0d48": "4",
      "1d54": "25",
      "2155": "200",
      "6435": "2",
      "f4ad": "33",
      "6c2b": "108",
      "adb5": "62",
      "e622": "0",
      "d1f0": "25",
      "8cf6": "0",
      "80ad": "0",
      "6fe4": "0",
      "a148": "2016-05-13",
      "6f55": "11:49",
      "3b7c": {
        "choice_values": [
          "2409"
        ],
        "other_values": []
      },
      "course": null
    }
  }
}

完整测试代码:

var json = @"{""record"":
  {""status"":""241"",
   ""id"":""a0de27a2-1447-4941-a3c0-8853e5682e85"",
   ""created_by"":""The Amazing Mongo"",
   ""project_id"":null,
   ""changeset_id"":""eeba5ba9-8305-4cb3-ad63-6d1bbff6b626"",
   ""valuetobeupdated1"":""some long value needs to go in here"",
   ""valuetobeupdated2"":""more data is needed here"",
   ""form_values"":      
    {""833b"":""00000"",
     ""683b"":""00000"",
     ""62b2"":""370"",
     ""6472"":""615"",
     ""e4fa"":""552"",
     ""7868"":""1"",
     ""0d48"":""4"",
     ""1d54"":""25"",
     ""2155"":""200"",
     ""6435"":""2"",
     ""f4ad"":""33"",
     ""6c2b"":""108"",
     ""adb5"":""62"",
     ""e622"":""0"",
     ""d1f0"":""25"",
     ""8cf6"":""0"",
     ""80ad"":""0"",
     ""6fe4"":""0"",
     ""a148"":""2016-05-13"",
     ""6f55"":""11:49"",
     ""3b7c"":{""choice_values"":[""2409""],
               ""other_values"":[]},
     ""valuetobeupdated3"":{""choice_values"":[""More information goes in here""],
                            ""other_values"":[]
                            },
     ""valuetobeupdated4"":{""choice_values"":[""more information here""],
                            ""other_values"":[]
                           },
     ""course"":null,}}}";

var root = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
root.record.valuetobeupdated1 = "Loonnnnnnnnnnnnnnnnnng value";
root.record.valuetobeupdated2 = "More data";
root.record.form_values.Remove("valuetobeupdated3");
root.record.form_values.Remove("valuetobeupdated4");

var output = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(output);

注意:我在json的末尾加了一个,否则不是有效的json。

关于C# 编辑动态字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37201423/

相关文章:

javascript - 从 PHP Mysql 查询中将数据提取到 Javascript 中

c# - 在 .Net 中将 Url 编码的表单数据转换为 JSON 有哪些选项

ruby-on-rails - Ruby 和编码转换

c# - 谷歌搜索结果html代码c#

c# - C# 中带有 FileSystemWatcher 的 Windows 服务

c# - 使用 [ClassInitilize]Attribute 优于静态构造函数(或 : TestContext wat?!)

c++ - 字符串类istream问题

c# - 使用 ReliableSqlConnection 填充数据表

java - 使用 Jackson 将 JSON 转换为 java 对象

android - Android开发+如何建立将保存在此文件夹中的文件夹和文件?