JsonConvert.DeserializeXmlNode 异常 - 名称中不能有冒号(:)
Posted
技术标签:
【中文标题】JsonConvert.DeserializeXmlNode 异常 - 名称中不能有冒号(:)【英文标题】:JsonConvert.DeserializeXmlNode exception - cannot have colon(:) in name 【发布时间】: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"
【问题讨论】:
【参考方案1】:您确定您的 JSON 格式正确吗?在将您提供的示例修复为格式正确的 JSON 后,我使用最新版本的 JSON.Net 尝试了以下代码。我没有看到将其转换为 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>
检查事项:
您的 JSON 有效吗?您可以使用jsonlint.com 进行检查。 您运行的是最新版本的 Json.Net 吗?您可以从 here 或在 Visual Studio 中使用 NuGet 获取最新信息。 在调用JsonConvert.DeserializeXmlNode
之前,您是否以任何方式操作JSON 字符串?如果是这样,你到底在做什么?我们需要详细信息。
如果这些不是问题,请编辑您的问题,以提供您遇到问题的完整 JSON,而不仅仅是零碎。
更新
现在您已经发布了完整的 JSON,我可以看到问题所在。您的 JSON 中有多个包含多个冒号的键。这是产生异常的最简单示例:
string json = @" "":jcr:data"": 54909 ";
XmlDocument xdoc = JsonConvert.DeserializeXmlNode(json, "root");
看起来有问题的键都是同一个——:jcr:data
。如果您可以控制 JSON 的来源,那么最好的解决方案是修复来源,使其在这些键的开头没有多余的冒号。
如果 JSON 来自您无法控制的第三方 Web 服务,一个简单的解决方法是在尝试将 JSON 转换为 XML 之前对 JSON 进行字符串替换。你可以这样做:
json = json.Replace(":jcr:data", "jcr:data");
我用你的完整 JSON 试了一下,效果很好。
【讨论】:
感谢您的回复,我验证了我的 json 文件,它是有效的,我收到的错误是:':' 字符,十六进制值 0x3A,不能包含在名称中。 部分 json 数据 "online.png": "jcr:uuid": "194f27d0-a549-493f-9c67-ac8f1ea436a5","jcr:createdBy": "admin", "jcr: mixinTypes": ["mix:referenceable"],"jcr:created":"2014 年 1 月 9 日星期四 05:23:09 GMT-0800","jcr:primaryType":"nt:file","jcr:content": "firstName": "Vendor","companyName": "EZ Systems", ":jcr:data": 1857,"phone": "888-123-0126","status": "Active","designation" :“APL”,“jcr:mimeType”:“image/png”,“lastName”:“Image6”,“jcr:lastModified”:“2014 年 1 月 9 日星期四 05:22:58 GMT-0800”,“jcr:primaryType ": "nt:unstructured","city": "海得拉巴" , 是否可以使用 linq 查询 jobject,并在任何深度搜索键值和数组名称?如果是,请帮助我编写查询,这样我就不必担心从 json 转换 xml 以及稍后在转换后的 xml 上使用 xpath 来执行搜索操作。 json 文件没有根元素,它以多个属性开头,并收集了有关图像的元数据,我发布了一个示例图像元数据 deserializedValue = CreateValueInternal(reader, objectType, contract, null, null, null, null) 处抛出异常;在 JsonSerializerInternalReader.cs 文件中以上是关于JsonConvert.DeserializeXmlNode 异常 - 名称中不能有冒号(:)的主要内容,如果未能解决你的问题,请参考以下文章