2011-01-11 43 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