Since you do not mind using another library, I would suggest NewtonSoft JSON.NET. There are classes there ( JObject , JArray , etc.) that can represent arbitrary JSON data as part of some strongly typed object. I used this to deserialize some big JSON, for which I was only interested in a small part. I could deserialize the whole JSON, change the part that was important and serialize, while keeping the irrelevant part untouched.
Here is a sample code to get the children string as a string, although this is an array in JSON. The important thing is that you can change the contents of other fields ( name and home ) and serialize the whole thing, and children in the JSON output will remain an array with the original content.
Assuming
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
here is an example code:
public class RootObject { public string name; public List<object> home; public JArray children; // I don't care what children may contain } class Program { static void Main(string[] args) { string sourceJSON = @"{""name"":""Chris"",""home"":[],""children"":[{""name"":""Belle""},{""name"":""O""}]}"; RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(sourceJSON); string partAsString = rootObject.children.ToString(Formatting.None); // partAsString is now: [{"name":"Belle"},{"name":"O"}] } }
source share