我有一个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.
更多推荐
发布评论