Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep JSON value as String?

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.

like image 606
Chris Lists Avatar asked Jul 13 '15 19:07

Chris Lists


People also ask

Can JSON be stored as a string?

JSON exists as a string — useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data.

How can I convert JSON to string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Are JSON values always strings?

JSON is always a string representation - it has to be parsed to create an object for use within JavaScript (or other languages) and once that happens JavaScript (or the other languages) treat the resulting object the same as any other object.


2 Answers

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"}]
    }
}
like image 166
Kuba Wyrostek Avatar answered Oct 21 '22 11:10

Kuba Wyrostek


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));
        }
    }
}
like image 44
Win Avatar answered Oct 21 '22 10:10

Win