JSON数组不会填充

时间:2010-07-21 18:23:59

标签: asp.net json arrays object serialization

由于某种原因,以下JSON字符串会在自定义对象数组中创建正确数量的记录,但不会使用和值填充数组中的对象。帮助赞赏!

JSON字符串

{ 
"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
}

反序列化程序

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?
}

自定义类

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 个答案:

答案 0 :(得分:2)

您的数组JSON包含一个对象,该对象具有一个名为CommitImporterGrid的属性。这不会出现在代码中的任何位置。我认为你需要从JSON中丢失这个{ "CommitImporterGrid":,以及相应的紧密括号。

答案 1 :(得分:1)

旧:(差)

{
    "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
}

新:

{
    "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
}

我上面的人有解释,我想我会告诉你正确的JSON。