Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this JSON returning as "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: ."}
like image 802
Prisoner ZERO Avatar asked Apr 07 '11 19:04

Prisoner ZERO


People also ask

What does invalid JSON primitive mean?

If it is a valid json object like {"foo":"foovalue", "bar":"barvalue"} then jQuery might not send it as json data but instead serialize it to foor=foovalue&bar=barvalue thus you get the error "Invalid JSON primitive: foo" Try instead setting the data as string.

Why is my JSON file invalid?

It means that the editor failed to get a response to the server or the response wasn't in a valid JSON format. Basically, if the editor can't communicate with the server, it will show this error message instead. To fix the problem, you essentially need to fix whatever is getting in the way of the communication.

What is JSON primitive?

JSON can represent (sub)values of four primitive data types and of two compound data types. The primitive data types are string, number, boolean, and null. There is no way to declare the data type of a JSON value; rather, it emerges from the syntax of the representation.


1 Answers

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.

like image 169
darioo Avatar answered Oct 16 '22 19:10

darioo