xml到json字符串,在数字中没有双qoutes,在c#中没有十进制类型(xml to json string without double qoutes in number and decimal

编程入门 行业动态 更新时间:2024-10-27 20:35:48
xml到json字符串,在数字中没有双qoutes,在c#中没有十进制类型(xml to json string without double qoutes in number and decimal type in c#)

我有一个XElement我试图转换为JSON字符串。 以下是我的XElement:

<myxml> <gstin>28GSDUH1331G155</gstin> <fp>122016</fp> <gt>3782969.01</gt> <b2b> <ctin>29GSDUH1331G155</ctin> <inv p2:Array="true"> <inum>S008502</inum> <idt>15-11-2016</idt> <val>10000.00</val> <pos>27</pos> <rchrg>N</rchrg> <prs>Y</prs> <od_num>8401</od_num> <od_dt>14-11-2016</od_dt> <etin>30GSDUH1331G155</etin> <itms p2:Array="true"> <num>1</num> <itm_det> <ty>G</ty> <hsn_sc>G1221</hsn_sc> <txval>10000.00</txval> <crt>1.00</crt> <camt>100.00</camt> <srt>1.00</srt> <samt>100.00</samt> <csrt>100.00</csrt> <csamt>100.00</csamt> </itm_det> </itms> </inv> </b2b> </myxml>

我正在使用此字符串jsonString = JsonConvert.SerializeXNode(myxml) ; 转换为json字符串,我得到了jsonstring

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}]}]}]}}"

我遇到的问题是整数和十进制值被反斜杠和双引号(\“\”)包围。 我不想在数字和小数上使用反斜杠双引号。

这是我想要作为输出的JSON字符串。

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":122016,\"gt\":3782969.01,\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":10000.00,\"pos\":27,\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":8401,\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":1,\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":10000.00,\"crt\":1.00,\"camt\":100.00,\"srt\":1.00,\"samt\":100.00,\"csrt\":100.00,\"csamt\":100.00}}]}]}]}}"

I have an XElement I am trying to convert into a JSON string. Below is my XElement:

<myxml> <gstin>28GSDUH1331G155</gstin> <fp>122016</fp> <gt>3782969.01</gt> <b2b> <ctin>29GSDUH1331G155</ctin> <inv p2:Array="true"> <inum>S008502</inum> <idt>15-11-2016</idt> <val>10000.00</val> <pos>27</pos> <rchrg>N</rchrg> <prs>Y</prs> <od_num>8401</od_num> <od_dt>14-11-2016</od_dt> <etin>30GSDUH1331G155</etin> <itms p2:Array="true"> <num>1</num> <itm_det> <ty>G</ty> <hsn_sc>G1221</hsn_sc> <txval>10000.00</txval> <crt>1.00</crt> <camt>100.00</camt> <srt>1.00</srt> <samt>100.00</samt> <csrt>100.00</csrt> <csamt>100.00</csamt> </itm_det> </itms> </inv> </b2b> </myxml>

I am using this string jsonString = JsonConvert.SerializeXNode(myxml); to convert in json string and i am getting the jsonstring

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}]}]}]}}"

The problem that i am running into is that integer and decimal values are being surrounded by backslashes and double quotes(\" \") . I don't want backslash double quotes on numbers and decimals.

This is the JSON string which i want as an output.

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":122016,\"gt\":3782969.01,\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":10000.00,\"pos\":27,\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":8401,\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":1,\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":10000.00,\"crt\":1.00,\"camt\":100.00,\"srt\":1.00,\"samt\":100.00,\"csrt\":100.00,\"csamt\":100.00}}]}]}]}}"

最满意答案

正如评论已经指出的那样 - 更多信息可能会有所帮助(因为您的XML目前无效)。

同样在将来,最好向您提出的问题展示最小,完整和可验证的示例 - 不仅要表明您已经尝试过,而且要给予帮助更多信息的人。

简单地检查调试器中的元素时,它实际上显示反斜杠

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":{\"ctin\":\"29GSDUH1331G155\",\"inv\":{\"@Array\":\"true\",\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":{\"@Array\":\"true\",\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}}}}}"

这些反斜杠只是作为转义字符插入,实际上并不存在。

如果您检查jsonString的实际值,您将找到您的预期值。 当您在调试器中查看jsonString时,您正在查看raw JSON string ,但在下面的屏幕截图中,您正在查看JSON对象的表示。

在此处输入图像描述

As the comments have already pointed out - more information would likely help (as your XML is currently invalid).

Also in the future it is always best to show Minimal, Complete, and Verifiable examples to the questions you are asking - not only to show that you have tried, but to give people who are helping more information.

When simply inspecting the element in the debugger as you did it does in-fact show backslashes

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":{\"ctin\":\"29GSDUH1331G155\",\"inv\":{\"@Array\":\"true\",\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":{\"@Array\":\"true\",\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}}}}}"

These backslashes are just inserted as escape characters and are NOT actually there.

If you inspect the actual values of jsonString you will find your expected value. When you are viewing the jsonString in the debugger you are looking at a raw JSON string, but in the below screenshot you are looking at a representation of a JSON object.

enter image description here

更多推荐

本文发布于:2023-08-04 18:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   类型   数字   xml   qoutes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!