2011-01-11 47 views
1
創建JProperty

我需要得到JSON從辭典中的LINQ JSON.NET

{ 
    "question":"q1", 
    "answers": { 
     1:"ans1", 
     2:"ans2", 
     3:"ans3" 
    } 
    "corr":[1,2] 
} 

這個表達式包含LINQ

JObject jsonContent = 
      new JObject(
       new JProperty("question", _question), 
       new JProperty("answers", 
        new JObject(
         from ans in _answers 
         select new JProperty (ans.Key.ToString(),ans.Value))), 
       new JProperty("corr", 
        new JArray(
         from ans in _correctAnswers 
         select ans))); 

其中

string _question; 
List<int> _correctAnswers; 
Dictionary<int, string> _answers; 

我有一個問題將字典轉換爲JProperty

System.ArgumentNullException: Value cannot be null. 
Parameter name: source 

UPD:設置所有的值。沒有空答案

UPD2:對不起。一切正常。這個問題在數據庫訪問層

回答

0

看起來有可能在字典中的答案是空

嘗試: select new JProperty (ans.Key.ToString(),ans.Value ?? string.Empty)

+0

的所有值設置。沒有空的答案 – 2011-01-11 07:55:52