【问题标题】:How to change JSON's value for List (or array) type?如何更改列表(或数组)类型的 JSON 值?
【发布时间】:2021-04-18 08:58:15
【问题描述】:
"title" : { "newTitle" : "Test"}
"tags" : { "newTags" : ["Tag1", "Tag2"] }
Newtonsoft.Json.Linq;

var json = JObject.Parse(json: json);
var title;  // "Test2" in title
List<string> tags; // "TagA", "TagB", "TagC" in tags


json["title"]["newTitle"] = title; // works well
json["tags"]["newTags"] = tags; // not work

我想要 JSON 结果如下:

"title" : { "newTitle" : "Test2"}
"tags" : { "newTags" : ["TagA", "TagB", "TagC"] }

我想修改 JSON 的一些值。 intstring 运行良好。但是,ListArray 不起作用。

请给我一个好的意见。

我使用了翻译器。所以写起来可能会很尴尬。

【问题讨论】:

    标签: c# json json.net


    【解决方案1】:

    假设您的 JSON 数据已定义对象模板,您可以根据您的 JSON 数据创建一个类。使用 Newtonsoft.Json,您可以将 JSON 反序列化为一个对象,然后更新该对象的属性值。

    注意:当访问对象的内部属性例如Title.NewTitleTags.NewTags时,您可能需要添加一些空检查以防止NullReferenceException

    第一种解决方案:转换为强类型对象

    public static void Main()
    {
        var json = "{\"title\" : { \"newTitle\" : \"Test\"}, \"tags\" : { \"newTags\" : [\"Tag1\", \"Tag2\"] }}";
            
        var inputObj = JsonConvert.DeserializeObject<JsonInput>(json);
        inputObj.Title.NewTitle = "Test2";
        inputObj.Tags.NewTags = new List<string> {"TagA", "TagB", "TagC"};
            
        Console.WriteLine(JsonConvert.SerializeObject(inputObj));
    }
    
    public class JsonInput
    {
        public Title Title {get;set;}
        public Tags Tags {get;set;}
    }
    
    public class Title
    {
        public string NewTitle {get;set;}
    }
    
    public class Tags
    {
        public List<string> NewTags {get;set;}
    }
    

    1st solution Code snippets and Output

    第二个解决方案:动态

    要更新数组,您需要将 List&lt;string&gt; 解析为 JArray 类型

    public static void Main()
    {
        var json = "{\"title\" : { \"newTitle\" : \"Test\"}, \"tags\" : { \"newTags\" : [\"Tag1\", \"Tag2\"] }}";
            
        var title = "Test2";  // "Test2" in title
        List<string> tags = new List<string> {"TagA", "TagB", "TagC"}; // "TagA", "TagB", "TagC" in tags
    
        dynamic root = JObject.Parse(json);
        JObject titleObj = (JObject)root["title"];
        titleObj["newTitle"] = title;
            
        JObject tagsObj = (JObject)root["tags"];
        tagsObj["newTags"] = JArray.FromObject(tags);
            
        Console.WriteLine(root);
    }
    

    2nd solution Code snippets and Output

    【讨论】:

    • 感谢您的回答。但这与我的意图完全不同。我将采用一些不固定的 JSON 值并对其进行修改。 JSON 有数百个变量,无法创建这样的类。
    • 嗨@이현월,我添加了第二个解决方案,它为JArray 更新动态对象中的 JSON 对象。你可以看看。谢谢。
    • 感谢您的好意。多亏了你,我才得以开阔眼界。
    • 不客气。如果此解决方案有助于解决您的问题,您也可以将其标记为答案。谢谢。
    【解决方案2】:

    试试这个

         var jsonObject=JObject.Parse(json);
    
        var newTitle =   "Test2";
        List<string> newTags = new List<string> { "TagA", "TagB", "TagC"};
        
        jsonObject["title"]["newTitle"]= newTitle; 
        jsonObject["tags"]["newTags"]= JArray.FromObject(newTags);
    

    结果

    {
      "title": {
        "newTitle": "Test2"
      },
      "tags": {
        "newTags": [
          "TagA",
          "TagB",
          "TagC"
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2020-06-08
      • 2018-05-28
      • 2012-06-08
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多