C#编辑动态字符串

时间:2016-05-13 04:43:38

标签: 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,}}"

所以我的问题是如何开始这个,我从来没有在正则表达式上表现出色,甚至不确定正则表达式是否可以做我想做的事情。 “有些长期值需要到这里”,其他值可以是任意长度。

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:3)

使用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。