【问题标题】:C# Editing Dynamic StringsC# 编辑动态字符串
【发布时间】:2016-05-13 04:43:37
【问题描述】:

我有一个问题摆在我面前,我什至不知道如何解决它。

背景是我有一个来自 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,}}"

所以我的问题是我该如何开始这个,我从来都不擅长正则表达式,甚至不确定正则表达式是否可以在这里做我想做的事情。 “Some long value need to go here”和其他值可以是任意长度。

任何建议将不胜感激。

【问题讨论】:

  • 你看过 JSON.net 吗?这将是比正则表达式更好的方法:newtonsoft.com/json/help/html/readingwritingjson.htm
  • 我同意。使用上面@IanMercer 链接中演示的 StringReader,或者如果可能,定义一个与传入的类型匹配的 C# 对象并反序列化为该类型。您提到数据是非常动态的,所以这可能是不可能的,但这是我的首选方法。
  • this 可能会引起您的兴趣
  • 除了添加或删除这些十六进制字段之外,一般路径是否不变?比如form_valueschoice_values的名字
  • @IanMercer 现在读到了。不是 100% 确定它会做我想做的事。

标签: c# json string


【解决方案1】:

使用Json.NETdynamic

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。

【讨论】:

  • 很棒的答案...我现在甚至可以将我的修改能力扩展到其他领域
猜你喜欢
  • 2017-11-30
  • 2017-06-18
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2017-08-27
相关资源
最近更新 更多