JSON.NET为什么要添加所有这些反斜杠 [英] Why is JSON.NET adding all these backslashes

查看:268
本文介绍了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.

这篇关于JSON.NET为什么要添加所有这些反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆