我需要使用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"
}
}
}
答案 0 :(得分:0)
您确定您的JSON格式正确吗?在修复了您提供的示例以正确格式化JSON之后,我使用最新版本的JSON.Net(5.0.8)尝试了以下代码。我没有看到任何将其转换为XML的异常。似乎Json.Net通过在转换期间从键中删除前缀来处理冒号。
class Program
{
static void Main(string[] args)
{
string json = @"
{
""jcr:type"" : ""nt:file"",
""jcr:content"" : [""jcr:uuid"", ""jfkjsaf""]
}";
XmlDocument xdoc = JsonConvert.DeserializeXmlNode(json, "root");
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (XmlTextWriter writer = new XmlTextWriter(sw))
{
writer.Formatting = System.Xml.Formatting.Indented;
xdoc.WriteTo(writer);
}
Console.WriteLine(sb.ToString());
}
}
输出:
<root>
<type>nt:file</type>
<content>jcr:uuid</content>
<content>jfkjsaf</content>
</root>
要检查的事项:
JsonConvert.DeserializeXmlNode
之前以任何方式操纵JSON字符串?如果是这样,你究竟在做什么?我们需要详细信息。如果这些问题不成问题,请编辑您的问题以提供您遇到问题的完整JSON,而不仅仅是点点滴滴。
现在您已经发布了完整的JSON,我可以看到问题。您的JSON中的密钥中包含多个冒号。以下是产生异常的最简单示例:
string json = @"{ "":jcr:data"": 54909 }";
XmlDocument xdoc = JsonConvert.DeserializeXmlNode(json, "root");
看起来有问题的密钥都是一样的 - :jcr:data
。如果您可以控制JSON的源代码,那么最好的解决方案是修复源代码,使其在这些键的开头没有额外的冒号。
如果JSON来自您无法控制的第三方Web服务,一个简单的解决方法是在尝试将其转换为XML之前对JSON执行字符串替换。你可以这样做:
json = json.Replace(":jcr:data", "jcr:data");
我用你的完整JSON尝试了这个,它工作得很好。