Why is JSON.NET adding all these backslashes

♀尐吖头ヾ 提交于 2019-12-11 02:45:18

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9087253/why-is-json-net-adding-all-these-backslashes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!