代码之家  ›  专栏  ›  技术社区  ›  Prisoner ZERO

JSON数组未填充

  •  1
  • Prisoner ZERO  · 技术社区  · 15 年前

    出于某种原因,下面的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 回复  |  直到 15 年前
        1
  •  1
  •   Bob Fincheimer    15 年前

    老:(坏)

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

        2
  •  2
  •   mackenir    15 年前

    数组json包含一个名为commitimportergrid的属性的对象。这不会出现在代码中的任何地方。我想你得把这个丢了 { "CommitImporterGrid": 来自JSON,以及相应的闭合花括号。