Json.NET添加反斜线而返回JSON序列化的字符串 [英] Json.NET adding backslash while returning json serialized string

查看:1700
本文介绍了Json.NET添加反斜线而返回JSON序列化的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Json.NET序列化的列表JSON字符串,但返回的字符串有反斜杠在其中,而这又是一个失败的JSON解析。

I am trying to serialize a list to json string using Json.NET but the return string has backslash within it, which in turn is failing a json parsing.

var x = from d in entities.Books.ToList()
        select new
        {
            ID = d.ID,
            BookName = d.BookName
        };
return JsonConvert.SerializeObject(x.ToList());

以上code返回

The above code returns

"[{\"ID\":1,\"BookName\":\"MVC Music Store - Tutorial - v3.0\"},{\"ID\":2,\"BookName\":\"Pro.ASP.NET.MVC.3.Framework\"},{\"ID\":3,\"BookName\":\"Application Architecture Guide v2\"},{\"ID\":4,\"BookName\":\"Gang of Four Design Patterns\"},{\"ID\":5,\"BookName\":\"CS4 Pocket Reference\"}]"

而失败,所有JSON解析。我怎样才能删除这些。

which fails all JSON parsing. How can I remove these.

推荐答案

没有。它没有

class Program
{
    class Book
    {
        public int ID;
        public string BookName;
    }

    static void Main()
    {
        var books = new List<Book> { new Book { ID = 1, BookName = "A" }, new Book { ID = 2, BookName = "B" } };

        var x = from d in books
        select new
        {
            ID = d.ID,
            BookName = d.BookName
        };

        string str = JsonConvert.SerializeObject(x.ToList());
        Console.WriteLine(str);
    }
}

可能有两个问题:

There could be two problems:

A)您正在寻找从调试器的结果。要检查这一点,把 JsonConvert 在临时变量(像我一样),看看它与调试器。点击沙漏的右箭头,选择文本展示台

A) You are looking at the result from the debugger. To check for this, Put the JsonConvert in a temporary variable (like I did) and look at it with the debugger. Click on the arrow right of the hourglass and select Text Visualizer.

B)的调用方法正在改变对象的再次到JSON,所以逃避一切。

B) The calling method is transforming the object again to Json, so escaping everything.

这篇关于Json.NET添加反斜线而返回JSON序列化的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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