Barka
Barka

Reputation: 8922

Why is JSON.NET adding all these backslashes

Please see:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;
namespace TestJson2
{
    class Program
    {
        private static List<string> myCollections;

        static void Main(string[] args)
        {
            myCollections = new List<string>();

            myCollections.Add("frog");
            myCollections.Add("dog");
            myCollections.Add("cat");

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.None;

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("id");
                jsonWriter.WriteValue("12345");

                jsonWriter.WritePropertyName("title");
                jsonWriter.WriteValue("foo");

                string animals = CollectionToJson();
                jsonWriter.WritePropertyName("animals");
                jsonWriter.WriteValue(animals);

                jsonWriter.WriteEndObject();
            }
            var result = sw.ToString();
        }
        private static string CollectionToJson()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.None;

                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("animals");
                jsonWriter.WriteStartArray();
                foreach (var animal in myCollections)
                {
                    jsonWriter.WriteValue(animal);
                }
                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();
            }
            return sw.ToString();
        }
    }


}

The result variable's content ends up being:

{"id":"12345","title":"foo","animals":"{\"animals\":[\"frog\",\"dog\",\"cat\"]}"}

now as the json hierarchical structure gets deeper (multiple layers that I am not showing here for brevity) the slashes become multiple: \\\. I understand that we need to escape the " so it does not terminate the string, but shouldn't the end user of this string just see the JSON without the backslashes? What am I doing wrong?

Thanks!

Upvotes: 2

Views: 3716

Answers (2)

Antony Scott
Antony Scott

Reputation: 21998

It would be far simpler to just create a C# object to represent your data and use the JsonSerializer to turn it into a json string.

Upvotes: 1

Marc B
Marc B

Reputation: 360642

You're embedding multiple independent json strings inside each other. The outer json writers have no idea that you built another json string inside, so they just see it as a plaintext string, not json, and have to escape the quotes.

instead of building json on json on json on...., build ONE data structure and pass that to a single JSON builder.

Upvotes: 7

Related Questions