Why is this JSON returning as “Invalid JSON primitive”?

微笑、不失礼 提交于 2019-12-22 04:35:18

问题


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: ."}

回答1:


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.




回答2:


"ClientKey": , has no value

http://www.jsonlint.com/



来源:https://stackoverflow.com/questions/5586555/why-is-this-json-returning-as-invalid-json-primitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!