使用JSON序列化后转义字符 [英] Escape characters after Serializing using Json

查看:2493
本文介绍了使用JSON序列化后转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Y是在一个奇怪的方式正在连载我的对象,而使用 newtonsoft.json 从ASP.Net的Web API?

Y is my object being serialized in a weird manner while using newtonsoft.json from ASP.Net Web API?

var s = JsonConvert.SerializeObject(request, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

输出字符串如下,

"{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\""

我想到 JSON格式字符串发送给第三方REST服务(它们只接受 JSON格式)。

I want to send Json format string to a third party rest service (they accept only Json format).

任何帮助深表感谢。

干杯
小号

推荐答案

有什么可担心的在这里。这是正确的序列化对象的JSON。它出现这样的,因为你正在阅读本在Visual Studio中通过将鼠标悬停变量查看它的价值,因为在C#\被用来表示在一个字符串。当你会写在一个文本文件中该值(只是为了测试实际值),你会看到什么是它的实际价值为:

There's nothing to be worried about here. This is the correct serialized object json. It is appearing like this because you are viewing this in visual studio by hovering variable to view its value, because in c# \" is used to represent a " in a string. When you will write this value in a text file(just to test actual value) , you will see what is it's actual value as:

  string json="{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\"";

    File.WriteAllText("c:\\tests on.txt",json) ;

您会看到文件中的JSON你真正想要的东西。

You will see the json in file what you actually want.

这篇关于使用JSON序列化后转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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