Replace part of a JSON with other (using a string token)

前端 未结 1 1295
深忆病人
深忆病人 2020-11-29 12:30

I\'m having an issue that I cant fix, I seek and seek in different places, but I still without find it.

See this code:

//I have 2 json to merge  

va         


        
1条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 13:23

    You can use SelectToken to select any item in the JSON object hierarchy. It supports JSONPath query syntax. It returns a JToken corresponding to the value of the selected item, or null if not found. That JToken in turn has a Replace(JToken replacement) method. Thus you can do:

    var o1 = JObject.Parse(json);
    var o2 = JObject.Parse(json2);
    
    var path = "client.spouse";
    o1.SelectToken(path).Replace(o2.SelectToken(path));
    
    var path2 = "client.email";
    o1.SelectToken(path2).Replace(o2.SelectToken(path2));
    

    0 讨论(0)
提交回复
热议问题