无效的Json基元

时间:2010-08-06 10:37:00

标签: c# asp.net json serialization javascriptserializer

你能帮我解决这个问题吗?我有一个asp.net应用程序,在此我使用Javascript序列化程序来序列化数据集,然后转换为列表。该代码如下所示。

JavaScriptSerializer json = new JavaScriptSerializer();
strJson = json.Serialize(aclDoc);

但是,在反序列化时,我得到了一个ArguementException,就像我的Json值的Invalid Json Primitives一样。我的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"}].

请帮我解决这个问题。提前致谢

4 个答案:

答案 0 :(得分:4)

您的输入字符串实际上是错误的 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"
    }
]

无法连接两个JSON字符串。在没有更多的JSON字符串连接之后准确说出你收到的内容。

我建议您验证http://www.jsonlint.com/中的JSON字符串。只需剪切并粘贴您需要验证的数据,然后单击“验证”按钮。

答案 1 :(得分:3)

直接回答这个问题,因为每个人都认为这是一个微软论坛,而不是直接回答。

该字符串作为2元素数组发送。你忘记了&#39; [&#39;在字符串的开头,表示包含的值是数组结构。

插入&#39; [&#39;在字符串的开头,错误应该消失。

答案 2 :(得分:0)

这是一个用于检查JSON对象的有用的小工具:

http://jsonviewer.codeplex.com/

答案 3 :(得分:0)

查看project.json中是否有任何//或注释行 删除它解决了我的同样问题