C# 使用JSON.NET中的LINQ从字典创建JProperty
我需要得到JSONC# 使用JSON.NET中的LINQ从字典创建JProperty,c#,linq,json.net,C#,Linq,Json.net,我需要得到JSON { "question":"q1", "answers": { 1:"ans1", 2:"ans2", 3:"ans3" } "corr":[1,2] } 此表达式包含LINQ JObject jsonContent = new JObject( new JProperty("question", _question),
{
"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)));
在哪里
UPD:所有值均已设置。没有空的答案
UPD2:对不起。一切正常。问题出在db访问层看起来字典中可能有一个空的答案 尝试:
选择新的JProperty(ans.Key.ToString(),ans.Value??string.Empty)
所有值都已设置。没有空的答案
string _question;
List<int> _correctAnswers;
Dictionary<int, string> _answers;
System.ArgumentNullException: Value cannot be null.
Parameter name: source