views:

41

answers:

2

For some reason the following JSON string creates the correct number of records in the custom objects array, but does NOT populate the objects in the array with and values. Help appreciated!

JSON String

{ 
"Grids": [{ "CommitImporterGrid": {"CostDivisionCode": "DL", "CostDivisionKey": 5, "CostDivisionName": "Direct Labor", "SourceType": "Contractor", "CommitDollars": 202, "CommitHours": 113.12, "PercentComplete": 50.00, "TaxRate": 0, "IohRate": 0.01, "ConditionerRate": 0}}],
"ProjectKey": 571, 
"AsOf": "1/1/2008 11:59:59 PM", 
"WbsKey": 1327, 
"FcrGroupKey": 26, 
"ContractorKey": 11
}

The Deserializer

protected void btnSave_Click(object sender, EventArgs e)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    JsonViewer view = serializer.Deserialize<JsonViewer>(txtJson.Value);

    // The LIST in the "view" object HAS records, but NO DATA?
}

The Custom Classes

public class JsonViewer
{
    public JsonViewer()
    { }

    public List<CommitImporterGrid> Grids { get; set; }

    public Int32 ProjectKey { get; set; }
    public String AsOf { get; set; }
    public Int32 WbsKey { get; set; }
    public Int32 FcrGroupKey { get; set; }
    public Int32 ContractorKey { get; set; } 
}



public class CommitImporterGrid
        {
            public CommitImporterGrid()
            { }

            public String CostDivisionCode { get; set; } 
            public Int32 CostDivisionKey { get; set; } 
            public String CostDivisionName { get; set; } 
            public String SourceType { get; set; } 
            public Decimal CommitDollars { get; set; } 
            public Decimal CommitHours { get; set; } 
            public Decimal PercentComplete { get; set; } 
            public Decimal TaxRate { get; set; } 
            public Decimal IohRate { get; set; } 
            public Decimal ConditionerRate { get; set; } 
        }
+2  A: 

Your array JSON contains an object with one property named CommitImporterGrid. This doesnt show up anywhere in your code. I think you need to lose this { "CommitImporterGrid": from the JSON, along with corresponding close curly bracket.

mackenir
Yeah, it looks to be an array with one object in it, not really much of an array at all....
Bob Fincheimer
FASTEST ANSWER EVER!!!! tHANKS SO MUCH...I corrected the JSON and it works GREAT!
Have a great day!
Glad to be of service [moseys along...].
mackenir
@Bob: For starters, your comment serves no purpose in answering anything! The REAL array is much larger. I simply parsed-out the extra rows to simplify make the example.
+1  A: 

Old: (Bad)

{
    "Grids": [
        { 
            "CommitImporterGrid": 
            {
                "CostDivisionCode": "DL", 
                "CostDivisionKey": 5, 
                "CostDivisionName": "Direct Labor", 
                "SourceType": "Contractor", 
                "CommitDollars": 202, 
                "CommitHours": 113.12, 
                "PercentComplete": 50.00, 
                "TaxRate": 0, 
                "IohRate": 0.01, 
                "ConditionerRate": 0
            }
        }
    ],
    "ProjectKey": 571,
    "AsOf": "1/1/2008 11:59:59 PM",
    "WbsKey": 1327,
    "FcrGroupKey": 26,
    "ContractorKey": 11
}

New:

{
    "Grids": [
        { 
            "CostDivisionCode": "DL", 
            "CostDivisionKey": 5, 
            "CostDivisionName": "Direct Labor", 
            "SourceType": "Contractor", 
            "CommitDollars": 202, 
            "CommitHours": 113.12, 
            "PercentComplete": 50.00, 
            "TaxRate": 0, 
            "IohRate": 0.01, 
            "ConditionerRate": 0
        }
    ],
    "ProjectKey": 571,
    "AsOf": "1/1/2008 11:59:59 PM",
    "WbsKey": 1327,
    "FcrGroupKey": 26,
    "ContractorKey": 11
}

The person above me has the explanation, thought i would show you the correct JSON though.

Bob Fincheimer