8

This is the JSON string:

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

I normally create custom object like this:

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 children as List,

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

How can I do that? I am using DataContractJSONSeriliazer.ReadObject method.

9
  • I cannot help you with DataContractJSONSeriliazer but I had similar need (to keep some part of JSON untouched) with Newtonsoft JSON.net. There I could use JObject or JArray class as a serialization target to keep a field a plain JSON. Maybe you could find something similar. Commented Jul 13, 2015 at 19:25
  • Please see if this could help you: msdn.microsoft.com/en-us/library/bb943471(v=vs.110).aspx Commented Jul 13, 2015 at 19:33
  • remove children as a serialize candidate, add a new property childrenstring or similar name, have the get property of the childrenstring return a string from the list of children. Commented Jul 13, 2015 at 19:37
  • @KubaWyrostek, I don't mind using different library, any sample codes for JSON.net? I like the keep the field a plain JSON maybe. Thanks!! Oh your second suggestion link is not exactly what I need 'cause I have to parse the children. I don't need to parse the children. Commented Jul 13, 2015 at 19:45
  • @TravisJ, sounds interesting, can you provide working sample codes? Thanks Commented Jul 13, 2015 at 19:48

2 Answers 2

7

Since you don't mind using another library I would propose NewtonSoft JSON.NET. There are classes there (JObject, JArray, etc.) which can represent arbitrary JSON data as a part of some strongly typed object. I was using this in order to deserialize some large JSON that only small part of was interesting to me. I could deserialize whole JSON, modify the part that was important and serialize back, keeping the irrelevant part untouched.

Here is as code sample to get your children as string, even though it is an array in JSON. What is important is that you could modify content of other fields (name and home) and serialize whole thing back, and children in JSON output would remain an array with original content.

Assuming

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

here's the code sample:

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"}]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect!, exactly what I am looking for. Marked as answer.
2

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

2 Comments

Thanks for your contribution. The other answer works perfectly for me in this case. I hope others will find your answer useful.
I'm glad that you figure out. Honestly, I like Kuba Wyrostek's answer more than mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.