JSON.NET为什么要添加所有这些反斜杠

问题描述:

请参阅:

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\"]}"}

现在,随着json层次结构变得更深(为简洁起见,此处不再显示多层),斜线变为多个:\\\.我知道我们需要转义,所以它不会终止字符串,但是此字符串的最终用户不应该只看到不带反斜杠的JSON吗?我在做什么错了?

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?

谢谢!

您正在相互嵌入多个独立的json字符串.外层json编写者不知道您在内部建立了另一个json字符串,因此他们只是将其视为纯文本字符串而不是json,并且必须转义引号.

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.

而不是在json on ..上在json上构建json,而是构建一个数据结构并将其传递给单个JSON构建器.

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