在 JSON.NET 中使用 LINQ 从字典创建 JProperty
我需要使用
{
"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)));
获取 JSON ,其中
string _question;
List<int> _correctAnswers;
Dictionary<int, string> _answers;
我在将 Dictionary 转换为 JProperty UPD 时遇到问题
System.ArgumentNullException: Value cannot be null.
Parameter name: source
:所有值均已设置。没有空答案
UPD2:抱歉。一切正常。问题出在数据库访问层
I need to get JSON
{
"question":"q1",
"answers": {
1:"ans1",
2:"ans2",
3:"ans3"
}
"corr":[1,2]
}
with this expression contains 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)));
where
string _question;
List<int> _correctAnswers;
Dictionary<int, string> _answers;
I have a problem with converting Dictionary into JProperty
System.ArgumentNullException: Value cannot be null.
Parameter name: source
UPD: The all of values are set. There is no null answer
UPD2: Sorry. All works fine. The problem was in db-access layer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来字典中可能有一个答案为 null
尝试:
选择新的 JProperty (ans.Key.ToString(),ans.Value ?? string.Empty)
Looks like there might be a answer in the Dictionary that is null
try:
select new JProperty (ans.Key.ToString(),ans.Value ?? string.Empty)
要将字典转换为 JProperty,我再次序列化并解析它:
我希望它有任何帮助。
To convert a dictionary to JProperty I serialize and parse it again:
I Hope it could be of any help.