2011-04-07 612 views
8

以下JSON不是反序列化。這顯然是因爲DECIMALS在保存JSON。我該如何解決?爲什麼這個JSON返回爲「無效的JSON基元」?

此初始JSON來自服務器和是有效的:

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

這隨後的JSON發送到服務器失敗:
一旦用戶編輯的形式中,數據被串行化客戶端-side併發送BACK ...在嘗試對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' 
    } 

此代碼拋出錯誤:

try 
    { 
     if (!String.IsNullOrEmpty(this.JSON)) 
     { 
      serializer = new JavaScriptSerializer(); 
      dialog = serializer.Deserialize<ProjectDecorator>(this.JSON); 
     } 
    } 
    catch (Exception ex) 
    { 
     // The message shows here 
    } 

引發的錯誤看起來像:

{"Invalid JSON primitive: ."} 

回答

11

不僅ClientKey沒有任何價值,但你通過不把鍵和值內雙引號("")冒着JSON區分有效性。

您的密鑰都正常,但string值必須用雙引號括起來。看看JSON網站,看看什麼是允許的,什麼不是。