이현월
이현월

Reputation: 25

How to change JSON's value for List (or array) type?

"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

I want the JSON result as below:

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

I want to modify some values of JSON. int or string worked well. However, the List or Array does not work.

Please give me a good opinion.

I used a translator. So the writing may be awkward.

Upvotes: 1

Views: 672

Answers (2)

Serge
Serge

Reputation: 43929

try this

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

result

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

Upvotes: 0

Yong Shun
Yong Shun

Reputation: 51350

Assume your JSON data has defined object template, you can create a class based on your JSON data. With Newtonsoft.Json, you deserialize your JSON into an object and next update the object's properties value.

Note: When access object's inner properties for example Title.NewTitle and Tags.NewTags, you may need to add some null checking for preventing NullReferenceException.

1st solution: Convert to strongly-typed object

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

2nd solution: With dynamic

To update array, you need parse your List<string> to JArray type

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

Upvotes: 1

Related Questions