【发布时间】:2014-01-29 10:36:11
【问题描述】:
我需要使用 NewtonSoft Json.Net 将 JSON 转换为 XML。 JSON 文件在某些对象键中包含带有冒号的数据。运行下面的代码时,会抛出异常。
XmlDocument xdoc = JsonConvert.DeserializeXmlNode(json, "root");
例外:The ':' character, hexadecimal value 0x3A, cannot be included in a name.
以下是 JSON 字符串中的一些键值对示例:
'jcr:type' : 'nt:file';
'jcr:content' ['jcr:uuid' : jfkjsaf]
这些只是从 JSON 字符串中随机挑选出来的;键和值都有带冒号的前缀。
如何让 Json.Net 忽略此异常并将 JSON 转换为 XML?
好的,这是我的json文件
{
"jcr:createdBy": "admin",
"jcr:created": "Mon Dec 30 2013 23:31:17 GMT-0800",
"jcr:primaryType": "sling:OrderedFolder",
"CustomerImage0": {
"id": 0,
"firstName": "Customer",
"state": "Andhra Pradesh",
"phone": "988-777-5550",
"status": "Active",
"lastName": "Image",
"country": "India",
"jcr:primaryType": "nt:unstructured"
},
"CustomerImage1": {
"id": 1,
"firstName": "Customer",
"state": "Andhra Pradesh",
"phone": "988-777-5551",
"status": "Active",
"lastName": "Image",
"country": "India",
"jcr:primaryType": "nt:unstructured"
},
"CustomerImage2": {
"id": 2,
"firstName": "Customer",
"state": "Andhra Pradesh",
"phone": "988-777-5552",
"status": "Active",
"lastName": "Image",
"country": "India",
"jcr:primaryType": "nt:unstructured"
},
"CustomerImage3": {
"id": 3,
"firstName": "Customer",
"state": "Andhra Pradesh",
"phone": "988-777-5553",
"status": "Active",
"lastName": "Image",
"country": "India",
"jcr:primaryType": "nt:unstructured"
},
"CustomerImage4": {
"id": 4,
"firstName": "Customer",
"state": "Andhra Pradesh",
"phone": "988-777-5554",
"status": "Active",
"lastName": "Image",
"country": "India",
"jcr:primaryType": "nt:unstructured"
},
"Nokia_7610_white.gif": {
"jcr:uuid": "65ce7b11-c5f1-4017-b27c-c455b1a710a1",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Fri Jan 03 2014 04:58:12 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 11886,
"phone": "888-123-0121",
"status": "Active",
"jcr:mimeType": "image/gif",
"designation": "APL",
"lastName": "Image1",
"jcr:lastModified": "Fri Jan 03 2014 04:57:59 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"Nokia_9300_close.gif": {
"jcr:uuid": "619ee9eb-d73a-4a65-b7e7-8bd5c2061412",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Fri Jan 03 2014 04:59:22 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 11797,
"phone": "888-123-0122",
"status": "Active",
"jcr:mimeType": "image/gif",
"designation": "APL",
"lastName": "Image2",
"jcr:lastModified": "Fri Jan 03 2014 04:59:09 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"Nokia_9500_close.gif": {
"jcr:uuid": "12a67241-aa11-4199-9b98-657406d777d6",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Fri Jan 03 2014 05:00:13 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 9873,
"phone": "888-123-0123",
"status": "Active",
"jcr:mimeType": "image/gif",
"designation": "APL",
"lastName": "Image3",
"jcr:lastModified": "Fri Jan 03 2014 05:00:00 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"Nokia_N90.gif": {
"jcr:uuid": "5a01d557-1430-4d5b-a9f8-5f836f2812a7",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Fri Jan 03 2014 05:00:57 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 8841,
"phone": "888-123-0124",
"status": "Active",
"jcr:mimeType": "image/gif",
"designation": "APL",
"lastName": "Image4",
"jcr:lastModified": "Fri Jan 03 2014 05:00:44 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"PreviewGif.gif": {
"jcr:uuid": "625112c6-dede-4333-8e78-fcdf474a190f",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Thu Jan 09 2014 05:19:39 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 26994,
"phone": "888-123-0125",
"status": "Active",
"designation": "APL",
"jcr:mimeType": "image/gif",
"lastName": "Image5",
"jcr:lastModified": "Thu Jan 09 2014 05:19:27 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"online.png": {
"jcr:uuid": "194f27d0-a549-493f-9c67-ac8f1ea436a5",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Thu Jan 09 2014 05:23:09 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 1857,
"phone": "888-123-0126",
"status": "Active",
"designation": "APL",
"jcr:mimeType": "image/png",
"lastName": "Image6",
"jcr:lastModified": "Thu Jan 09 2014 05:22:58 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
},
"Hot_Sale_Nike.pdf": {
"jcr:uuid": "52084540-b409-45cd-909b-9702742428b1",
"jcr:createdBy": "admin",
"jcr:mixinTypes": [
"mix:referenceable"
],
"jcr:created": "Fri Jan 10 2014 01:30:48 GMT-0800",
"jcr:primaryType": "nt:file",
"jcr:content": {
"firstName": "Vendor",
"companyName": "E Z Systems",
":jcr:data": 54909,
"phone": "888-123-0222",
"status": "Active",
"designation": "APL",
"jcr:mimeType": "application/pdf",
"lastName": "Pdf2",
"jcr:lastModified": "Fri Jan 10 2014 01:30:32 GMT-0800",
"jcr:primaryType": "nt:unstructured",
"city": "Hyderabad"
}
}
}
【问题讨论】:
标签: c# asp.net xml json json.net