为什么此 JSON 返回“无效的 JSON 原语”?

发布于 2024-10-30 18:36:27 字数 1769 浏览 1 评论 0原文

以下 JSON 未反序列化。显然是因为 DECIMALS 中保存的是 JSON。我该如何解决这个问题?

此初始 JSON 来自服务器并且有效:

    {
    "AppropriationAmount": 25000000, 
    "AppropriationHours": 56300, 
    "ArrThreshold": 11, 
    "ClientKey": 24, 
    "Description": 'Find and incarcerate the escaped prisoner', 
    "DirectHours": 50000, 
    "EndDate": '3/31/2011', 
    "EngineeringHours": 4000, 
    "IndirectHours": 2000, 
    "Key": 1589, 
    "Number": '0', 
    "OtherHours": 300, 
    "ProductivityCurveType": 'BurnedEarned', 
    "ProjectManager": 'Doctor Who', 
    "ProjectName": 'Prisoner ZERO', 
    "StartDate": '5/1/2010' 
    }

发送到服务器的后续 JSON 失败:
一旦用户编辑表单,数据就会在客户端序列化并发送回...尝试反序列化 JSON 时会失败。

    {
    "AppropriationAmount": 56300.00, 
    "AppropriationHours": 25000000.00, 
    "ArrThreshold": 11.00, 
    "ClientKey": , 
    "Description": 'Find and incarcerate the escaped prisoner', 
    "DirectHours": 50000.00, 
    "EndDate": '3/31/2011', 
    "EngineeringHours": 4000.00, 
    "IndirectHours": 2000.00, 
    "Key": 1589, 
    "Number": '0', 
    "OtherHours": 300.00, 
    "ProductivityCurveType": 'BurnedEarned', 
    "ProjectManager": 'Doctor Who', 
    "ProjectName": 'Prisoner ZERO', 
    "StartDate": '5/1/2010' 
    }

此代码抛出错误:

    try
    {
        if (!String.IsNullOrEmpty(this.JSON))
        {
            serializer = new JavaScriptSerializer();
            dialog = serializer.Deserialize<ProjectDecorator>(this.JSON);
        }
    }
    catch (Exception ex)
    {
        // The message shows here
    }

抛出的错误如下所示:

{"Invalid JSON primitive: ."}

The following JSON is not deserializing. It's obviously because the DECIMALS in the saves JSON. How do I fix this?

This initial JSON comes from the server and IS VALID:

    {
    "AppropriationAmount": 25000000, 
    "AppropriationHours": 56300, 
    "ArrThreshold": 11, 
    "ClientKey": 24, 
    "Description": 'Find and incarcerate the escaped prisoner', 
    "DirectHours": 50000, 
    "EndDate": '3/31/2011', 
    "EngineeringHours": 4000, 
    "IndirectHours": 2000, 
    "Key": 1589, 
    "Number": '0', 
    "OtherHours": 300, 
    "ProductivityCurveType": 'BurnedEarned', 
    "ProjectManager": 'Doctor Who', 
    "ProjectName": 'Prisoner ZERO', 
    "StartDate": '5/1/2010' 
    }

This subsequent JSON sent to the server FAILS:
Once the user edits the form, the data is serialized client-side and sent BACK...where it (then) fails upon attempting to de-serialize the JSON.

    {
    "AppropriationAmount": 56300.00, 
    "AppropriationHours": 25000000.00, 
    "ArrThreshold": 11.00, 
    "ClientKey": , 
    "Description": 'Find and incarcerate the escaped prisoner', 
    "DirectHours": 50000.00, 
    "EndDate": '3/31/2011', 
    "EngineeringHours": 4000.00, 
    "IndirectHours": 2000.00, 
    "Key": 1589, 
    "Number": '0', 
    "OtherHours": 300.00, 
    "ProductivityCurveType": 'BurnedEarned', 
    "ProjectManager": 'Doctor Who', 
    "ProjectName": 'Prisoner ZERO', 
    "StartDate": '5/1/2010' 
    }

This code throws the Error:

    try
    {
        if (!String.IsNullOrEmpty(this.JSON))
        {
            serializer = new JavaScriptSerializer();
            dialog = serializer.Deserialize<ProjectDecorator>(this.JSON);
        }
    }
    catch (Exception ex)
    {
        // The message shows here
    }

The Error thrown looks like:

{"Invalid JSON primitive: ."}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-11-06 18:36:27

ClientKey 不仅没有值,而且如果不将键和值放在双引号 ("") 内,还会面临 JSON 有效性的风险。

您的键没问题,但 string 值必须用双引号引起来。查看 JSON 网站,了解允许的内容和不允许的内容。

Not only does ClientKey have no value, but you're risking JSON validness by not putting keys and values inside double quotations marks ("").

Your keys are OK, but string values must be surrounded by double quotes. Take a look at JSON website to see what's allowed and what's not.

一抹微笑 2024-11-06 18:36:27

"ClientKey": ,没有值

http://www.jsonlint.com/

"ClientKey": , has no value

http://www.jsonlint.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文