C# 返回特定格式的json Web API,Linq
我需要帮助创建json。我有两个模型,即问题库和问题选项。 QUESTIONOPTIONS包含QUESTIONBANK中问题的多个选项C# 返回特定格式的json Web API,Linq,c#,linq,entity-framework-6,asp.net-web-api2,C#,Linq,Entity Framework 6,Asp.net Web Api2,我需要帮助创建json。我有两个模型,即问题库和问题选项。 QUESTIONOPTIONS包含QUESTIONBANK中问题的多个选项 public partial class QUESTIONBANK { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] publi
public partial class QUESTIONBANK
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public QUESTIONBANK()
{
this.QUESTIONOPTIONS = new HashSet<QUESTIONOPTION>();
}
public int QID { get; set; }
public string QUESTION { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<QUESTIONOPTION> QUESTIONOPTIONS { get; set; }
}
public partial class QUESTIONOPTION
{
public int OPTIONID { get; set; }
public Nullable<int> QID { get; set; }
public string OPTIONTEXT { get; set; }
public Nullable<bool> ISANSWER { get; set; }
public virtual QUESTIONBANK QUESTIONBANK { get; set; }
}
我得到的json如下所示
[
{
"q": "Accounting provides information on",
"a": "False",
"options": "Cost and income for managers"
},
{
"q": "Accounting provides information on",
"a": "False",
"options": " Companys tax liability for a particular year"
},
{
"q": "Accounting provides information on",
"a": "False",
"options": "Financial conditions of an institution"
},
{
"q": "Accounting provides information on",
"a": "True",
"options": " All of the above"
},
{
"q": "The long term assets that have no physical existence but are rights that have value is known as",
"a": "False",
"options": "Current assets"
},
{
"q": "The long term assets that have no physical existence but are rights that have value is known as",
"a": "False",
"options": "Fixed assets"
},
{
"q": "The long term assets that have no physical existence but are rights that have value is known as",
"a": "True",
"options": "Intangible assets"
},
{
"q": "The long term assets that have no physical existence but are rights that have value is known as",
"a": "False",
"options": "Investments"
},
{
"q": "The assets that can be converted into cash within a short period (i.e. 1 year or less) are known as",
"a": "True",
"options": "Current assets"
},
{
"q": "The assets that can be converted into cash within a short period (i.e. 1 year or less) are known as",
"a": "False",
"options": " Fixed assets"
},
{
"q": "The assets that can be converted into cash within a short period (i.e. 1 year or less) are known as",
"a": "False",
"options": "Intangible assets"
},
{
"q": "The assets that can be converted into cash within a short period (i.e. 1 year or less) are known as",
"a": "False",
"options": "Investments"
},
{
"q": "Patents, Copyrights and Trademarks are",
"a": "False",
"options": " Current assets"
},
{
"q": "Patents, Copyrights and Trademarks are",
"a": "False",
"options": " Fixed assets"
},
{
"q": "Patents, Copyrights and Trademarks are",
"a": "True",
"options": "Intangible assets"
},
{
"q": "Patents, Copyrights and Trademarks are",
"a": "False",
"options": "Investments"
},
{
"q": "The following is not a type of liability",
"a": "True",
"options": "Short term"
},
{
"q": "The following is not a type of liability",
"a": "False",
"options": "Current"
},
{
"q": "The following is not a type of liability",
"a": "False",
"options": "Fixed"
},
{
"q": "The following is not a type of liability",
"a": "False",
"options": "Contingent"
}
]
然而,我需要以下格式的json,其中问题选项需要以逗号分隔的形式检索,并且ISANSWER的标记为true的选项应该是“a”
quizData:{
“问题”:[{
“q”:“查看以下选择器:$(\“div\”)
它选择了什么?”,
“a”:“所有div元素”,
“选择”:[
“所有div元素”,
“第一个div元素”,
类为“div”的所有元素
]
}, {
“q”:“以下哪项是正确的”,
“a”:“jQuery是一个JavaScript库”,
“选择”:[
“jQuery是一个JSON库”,
“jQuery是一个JavaScript库”
]
}, {
“q”:“jQuery使用CSS选择器选择元素?”,
“a”:“正确”,
“选择”:[
“真的”,
“假”
]
}, {
“q”:“jQuery使用哪个符号作为jQuery的快捷方式?”,
“a”:“美元符号”,
“选择”:[
“%符号”,
“美元符号”,
“标志”
]
}, {
“q”:“jQuery是客户端脚本库还是服务器脚本库?”,
“a”:“客户端脚本编写”,
“选择”:[
“客户端脚本”,
“服务器脚本”,
]
}]
}
非常感谢你的帮助。提前感谢您可以聚合数据库中的结果,并将其包装为匿名类型
var aggr = qSet.GroupBy(x => x.q)
.Select(t => new {
q = t.Key,
a = t.FirstOrDefault(ans => ans.a == "True").options,
options = t.Select(ans => ans.options).ToList() });
var result = new { quizData = new { questions = aggr }};
return Request.CreateResponse(HttpStatusCode.OK, result);
您可以聚合数据库中的结果,并将其包装为匿名类型
var aggr = qSet.GroupBy(x => x.q)
.Select(t => new {
q = t.Key,
a = t.FirstOrDefault(ans => ans.a == "True").options,
options = t.Select(ans => ans.options).ToList() });
var result = new { quizData = new { questions = aggr }};
return Request.CreateResponse(HttpStatusCode.OK, result);
首先,这与Web API、JSON甚至EntityFramwork无关 您有一个逻辑错误,该错误在针对内存中集合的查询中以完全相同的方式显示,并且从未序列化 输出正是预期的结果 然而,修正很容易 下面的内容将实现此目的
public IEnumerable<QuestionViewModel> GetQUESTIONBANKs()
{
return from question in db.QUESTIONBANKs
join option in db.QUESTIONOPTIONS
on question.ID equals option.QID
into questonOptions
select new QuestionViewModel
{
Q = question.QUESTION,
A = questionOptions.First(o => o.ISANSWER).OPTIONTEXT,
Options = from o in questionOptions select o.OPTIONTEXT
};
}
首先,这与Web API、JSON甚至EntityFramwork无关 您有一个逻辑错误,该错误在针对内存中集合的查询中以完全相同的方式显示,并且从未序列化 输出正是预期的结果 然而,修正很容易 下面的内容将实现此目的
public IEnumerable<QuestionViewModel> GetQUESTIONBANKs()
{
return from question in db.QUESTIONBANKs
join option in db.QUESTIONOPTIONS
on question.ID equals option.QID
into questonOptions
select new QuestionViewModel
{
Q = question.QUESTION,
A = questionOptions.First(o => o.ISANSWER).OPTIONTEXT,
Options = from o in questionOptions select o.OPTIONTEXT
};
}
您是否尝试过使用自定义Json输出格式化程序?使您能够更好地控制如何显示输出对象=>基本上可以转换为json(或xml/其他格式)您可以使用LINQ group by clauseTake从数据库中复制条目并在C中按它们分组#我不确定是否使用实体框架进行分组,但熟悉存储过程~您是否尝试过使用自定义Json输出格式化程序?让您能够更好地控制如何显示输出对象=>您基本上可以转换为您想要的json(或xml/其他格式)使用LINQ group by clauseTake可以从DB中复制条目,并在C中按它们分组35;我不确定是否要使用实体框架,但熟悉存储过程~No,我的意思是A应该包含正确答案选项,不是真或假否,我的意思是A应该包含正确答案选项,不是真或假
from question in db.QUESTIONBANKs
select new QuestionViewModel
{
Q = question.QUESTION,
A =questionOptions.First(o => o.ISANSWER).OPTIONTEXT,
Options = from o in questionOptions select o.OPTIONTEXT
}