如何删除c#中括号和结束括号json之前的倒置逗号 [英] How to remove inverted comma before bracket and end bracket json in c#

查看:167
本文介绍了如何删除c#中括号和结束括号json之前的倒置逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{ResCode:1,ResStrDisplay:{EmpId:16,FullName:GTPTEST,名称:软件工程师,LastActivity:登录在02 / 12/2016 15:23来自IP地址:115.118.170.121}}

我得到了这个输出。

我想要跟随输出。

{ResCode:1,ResStrDisplay:{EmpId:16,FullName:GTPTEST,指定:软件工程师,LastActivity:登录于02/12 / 2016 15:23来自IP地址:115.118.170.121}}



{ResCode:1,ResStrDisplay:{...}}我想要删除这个标记。

我想删除{..}这个marks.before支架和支架后

这是我的代码





{"ResCode":1,"ResStrDisplay":"{"EmpId":16,"FullName":"GTPTEST","Designation":"Software Engineer","LastActivity":"Sign in recorded at 02/12/2016 15:23 from IP address: 115.118.170.121"}"}
I have get this output.
I want following output.
{"ResCode":1,"ResStrDisplay":{"EmpId":16,"FullName":"GTPTEST","Designation":"Software Engineer","LastActivity":"Sign in recorded at 02/12/2016 15:23 from IP address: 115.118.170.121"}}

{"ResCode":1,"ResStrDisplay":"{...}"}i want to remove this " " marks.
I want to remove "{..}" this "" marks.before bracket and after bracket
this is my code


WebEmployees ObjWebEmp = new WebEmployees();
                   ObjWebEmp.EmpId = objUser.UserId;
                   ObjWebEmp.FullName = objUser.FullName;
                   ObjWebEmp.Designation = objUser.Designation;
                   ObjWebEmp.LastActivity = lastactivity;
                   um.ResCode = 1;
                   um.ResStrDisplay = new JavaScriptSerializer().Serialize(ObjWebEmp);





我尝试过:





What I have tried:

WebEmployees ObjWebEmp = new WebEmployees();
                   ObjWebEmp.EmpId = objUser.UserId;
                   ObjWebEmp.FullName = objUser.FullName;
                   ObjWebEmp.Designation = objUser.Designation;
                   ObjWebEmp.LastActivity = lastactivity;
                   um.ResCode = 1;
                   um.ResStrDisplay = new JavaScriptSerializer().Serialize(ObjWebEmp);

推荐答案

在open括号({)之前有一个引号()这一事实通常意味着该值本身就是一个json字符串,这正是你正在做的事情。当你反序列化时字符串你应该允许ResStrDisplay成为一个字符串,然后将其反序列化到对象中。



问题是json序列化将包含所需的转义字符所以这样做两次将添加进一步的转义字符。如果你试图通过操纵字符串反序列化,你将不得不考虑到这一点。



另一个选项(和我使用的那个)是不预先序列化ResStrDisplay。如果你把它作为一个对象,那么整个WebEmployees对象的序列化将产生你期望的格式。
The fact that there is a quotation mark (") before the open bracket ("{") usually mean that the value is itself a json string, and this is infact exactly what you are doing. When you deserialize the string you should allow ResStrDisplay to be a string and then deserialize that into the object.

The issue is that a json serialization will include escape chars where needed so doing it twice will add further escape chars. if you try to deserialize by manipulating the string you will have to take that into account.

Another option (and the one I use) is to not pre-serialize ResStrDisplay. If you leave it as an object then the serialization of the whole WebEmployees object will produce the format you expect.


这篇关于如何删除c#中括号和结束括号json之前的倒置逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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