c# - 在 JSON.NET 中使用 LINQ 从字典创建 JProperty

我需要获取 JSON

{
   "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;

我在将 Dictionary 转换为 JProperty 时遇到问题

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

更新:所有值都已设置。没有空答案

UPD2:抱歉。一切正常。问题出在数据库访问层

最佳答案

看起来字典中可能有一个答案为空

尝试: 选择新的 JProperty (ans.Key.ToString(),ans.Value ?? string.Empty)

https://stackoverflow.com/questions/4655152/

相关文章:

c++ - 随机数生成的快速模数替换是什么?

visual-studio-2010 - TFS Team Build MSTest 运行持续时间太

.net - .NET 资源文件字符串是否被保留?

java - 为什么当我在另一台计算机上加载我的工作区时 Eclipse 显示错误?

asp.net - "Invalid use of response filter"压缩来自 IHt

unicode - 无法使用自定义操作将 MSI 安装到用户名中包含非 ascii 字符的非管理员

r - 如何使用 RODBC 或 RCurl 从 R 中受密码保护的 Sharepoint 2007

.net - ToString() 和调试器的字符串可视化工具

php - soap 信封是如何生成的,为什么 .NET 和 php 客户端会生成/接收不同的 so

maven-3 - mvn clean install + java.lang.NoClassDef