无效的 Json 基元
你能帮我解决这个问题吗?我有一个 asp.net 应用程序,在这个应用程序中,我使用 Javascript 序列化程序来序列化数据集,然后转换为列表。该代码如下所示。
JavaScriptSerializer json = new JavaScriptSerializer();
strJson = json.Serialize(aclDoc);
但是,在反序列化时,我遇到了一个 ArguementException,例如带有我的 Json 值的无效 Json 基元。我的 json 值是
[{"Id":"F79BA508-F208-4C37-9904-DBB1DEDE67DB","App_Id":"ScriptFlow","Name":"New form","FriendlyName":"","Read":"Revoke","ReadRule":"a353776f-cbdc-48b7-a15b-4a2316d19b05","Update":"Grant","UpdateRule":"be30c34e-33ec-4c0a-9f09-4fd483f5f1b9","Create":"Revoke","CreateRule":"898dce4d-4709-45b6-8942-d7efb07cbd86","Delete":"Revoke","DeleteRule":"aa14d435-dec8-4ade-ad9b-830ae5ee15d0"}][{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}]
反序列化的代码是:
JavaScriptSerializer json = new JavaScriptSerializer();
lstDoc = json.Deserialize<List<ACLDocument>>(value);
return lstDoc;
其中 lstDoc 是我的类类型的列表集合
我得到这样的异常:
Invalid JSON primitive:
{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}].
请帮助我解决此问题。提前致谢
Could you help me for resolving this issue. I have one asp.net application, in this i am using Javascript serializer for serializing a dataset followed by convertion to the list. That code is shown below.
JavaScriptSerializer json = new JavaScriptSerializer();
strJson = json.Serialize(aclDoc);
But, at the time of deserializing i got one ArguementException like Invalid Json Primitives with my Json value. My json value is
[{"Id":"F79BA508-F208-4C37-9904-DBB1DEDE67DB","App_Id":"ScriptFlow","Name":"New form","FriendlyName":"","Read":"Revoke","ReadRule":"a353776f-cbdc-48b7-a15b-4a2316d19b05","Update":"Grant","UpdateRule":"be30c34e-33ec-4c0a-9f09-4fd483f5f1b9","Create":"Revoke","CreateRule":"898dce4d-4709-45b6-8942-d7efb07cbd86","Delete":"Revoke","DeleteRule":"aa14d435-dec8-4ade-ad9b-830ae5ee15d0"}][{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}]
The code for deserializing is:
JavaScriptSerializer json = new JavaScriptSerializer();
lstDoc = json.Deserialize<List<ACLDocument>>(value);
return lstDoc;
where lstDoc is a List Collection of type of my class
I got the exception like this:
Invalid JSON primitive:
{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}].
Please help me for resolving this issue. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的输入字符串实际上是一个错误 JSON 字符串。您的输入由两个正确的 JSON 字符串组成:
但是
您无法连接两个 JSON 字符串。确切地说,在这样的连接之后,您收到的不仅仅是一个 JSON 字符串。
我建议您验证 http://www.jsonlint.com/ 中的 JSON 字符串。只需剪切并粘贴您需要验证的数据,然后单击“验证”按钮即可。
Your input string is really a wrong JSON string. You input consist from two correct JSON strings:
and
but you can not concatenate two JSON strings. To say exactly what you receive after such concatenating in not more a JSON string.
I recommend you to verify JSON strings in http://www.jsonlint.com/. Just cut and paste the data which you need to verify and click "Validate" button.
直接回答问题,因为大家都认为这是微软论坛而不直接回答。
该字符串作为 2 元素数组发送。您忘记了字符串开头的“[”,它表示包含的值是一个数组结构。
在字符串的开头插入“[”,错误就会消失。
To answer the question directly, since everyone thinks this is a Microsoft forum and not answering directly.
The string is sent as a 2 element array. You forgot the '[' in the beginning of the string which denotes that the containing values are an array structure.
Insert the '[' in the beginning of the string and the error should go away.
这是一个用于检查 JSON 对象的有用小工具:
http://jsonviewer.codeplex.com/
This is a useful little tool for examining your JSON objects:
http://jsonviewer.codeplex.com/
查看project.json 中是否有任何 // 或注释行
删除它为我解决了同样的问题
See if you have any // or commented lines in project.json
Removing this has solved the same problem for me