How to save JSON value as String?

This is the JSON string:

{"name":"Chris","home":[],"children":[{"name":"Belle"},{"name":"O"}]} 

I usually create a custom object as follows:

 public class Child { public string name { get; set; } } public class RootObject { [DataMember] public string name { get; set; } [DataMember] public List<object> home { get; set; } [DataMember] public List<Child> children { get; set; } } 

But now I don’t want the children to be like List,

I just want to write / serialize children as String, not Child. This means that I just don't mind holding this part: [{"name": "Belle"}, {"name": "O"}] as STRING, NOT Array / List.

How can i do this? I am using the DataContractJSONSeriliazer.ReadObject method.

+6
source share
2 answers

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"}] } } 
+3
source

Based on my understanding of your question, you can use Json.Net and add a dummy property.

enter image description here

 internal class Program { private static void Main(string[] args) { string json = "{\"name\":\"Chris\",\"home\":[],\"children\":[{\"name\":\"Belle\"},{\"name\":\"O\"}]}"; RootObject result = JsonConvert.DeserializeObject<RootObject>(json); Console.ReadLine(); } } public class Child { [JsonProperty(PropertyName = "name")] public string Name { get; set; } } public class RootObject { [JsonProperty(PropertyName = "name")] public string Name { get; set; } [JsonProperty(PropertyName = "home")] public List<object> Home { get; set; } [JsonProperty(PropertyName = "children")] public List<Child> ChildrenCollection { get; set; } [JsonIgnore] public string Children { get { // You can format the result the way you want here. return string.Join(",", ChildrenCollection.Select(x => x.Name)); } } } 
+1
source

All Articles