Asp.net

為什麼這個 JSON 返回為“無效的 JSON 原語”?

  • April 7, 2011

以下 JSON 未反序列化。這顯然是因為保存 JSON 中的 DECIMALS。我該如何解決?

這個初始 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 失敗:

一旦使用者編輯表單,數據就會在客戶端序列化並發送回……它(然後)在嘗試反序列化 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: ."}

不僅沒有ClientKey任何價值,而且由於不將鍵和值放在雙引號 ( "") 內,您會冒 JSON 有效性的風險。

您的鍵沒問題,但string值必須用雙引號括起來。看看JSON網站,看看什麼是允許的,什麼是不允許的。

“ClientKey”: , 沒有值

http://www.jsonlint.com/

引用自:https://stackoverflow.com/questions/5586555