如何将以下查询结果转换为JSON字符串 [英] How to convert Following Query result into JSON string

查看:98
本文介绍了如何将以下查询结果转换为JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RankrEntities context = new RankrEntities();
            var query = (from EQ in context.tbl_ExamQuestion
                         join Q in context.tbl_Question on EQ.QuestionID equals   
                         Q.QuestionID
                         where EQ.ExamID == 1 && EQ.Status.Equals("A") &&   
                         Q.Status.Equals("A")
                         select new
                         {
                             Q.QuestionID,
                             Q.Question,
                             options = from QO in context.tbl_Question_Option where      
                                       QO.Status.Equals("A") && QO.QuestionID ==  
                                       Q.QuestionID orderby QO.QuestionOrder select   
                                       new { QO.QuestionOID,  QO.Options },
                             ans = from QO in context.tbl_Question_Option where  
                                   QO.Status.Equals("A") && QO.QuestionID == 
                                   Q.QuestionID && QO.IsCorrect.Equals("Y") select   
                                   QO.QuestionOID,
                         }).ToList().Select(x => new { x.QuestionID, x.Question, obj =   
                          (string.Join(" ,", x.options)).AsEnumerable(),x.ans });
                      var ser = new JavaScriptSerializer();
                      string temp = ser.Serialize(query);





当我通过ajax方法调用此函数时的结果如下:



Object {QuestionID:1,Question:DBMS充当两个组件之间的接口企业级数据库系统?,obj:{QuestionOID = 1,Options = Database application ... OID = 4,Options = Database application and SQL},ans:Array [1]}问题:DBMS充当企业级数据库系统的两个组件之间的接口?







在上面的查询中,options返回多个QuestionOID和Options。

现在我们使用string.Join将上面的结果分隔为,。此结果包含=符号,该符号无法转换为JSON字符串。有没有从上面的查询中替换string.Join的选项?或者我们如何将上述查询与选项的结果一起转换?在此先感谢:)



My result when I call this function by ajax method is as follows :

Object {QuestionID: 1, Question: "The DBMS acts as an interface between what two components of an enterprise-class database system?", obj: "{ QuestionOID = 1, Options = Database application …OID = 4, Options = Database application and SQL }", ans: Array[1]}Question: "The DBMS acts as an interface between what two components of an enterprise-class database system?"



In above query, "options" returns multiple QuestionOID and Options.
Now we are using string.Join to separate above result by "," . This result contains "=" sign which cannot be converted into JSON string. Is there any option to replace string.Join from above query? OR how we can convert above query along with result of "options" ??? Thanks in advance :)

推荐答案

我们尝试过JSON.Encode了吗?检查下面的链接。



https://msdn.microsoft.com/en-us/library/system.web.helpers.json.encode(v = vs.111)的.aspx [ ^ ]
Have we tried JSON.Encode yet ? Check link below.

https://msdn.microsoft.com/en-us/library/system.web.helpers.json.encode(v=vs.111).aspx[^]


您好,



您可以删除连接部分并仍然可以将其转换到包含JSON的javascript数组。



我试图减少你的代码

Hi,

You can remove the join part and still be able to convert it to javascript array containing JSON.

I have tried to reduce your code
select new
                         {
                             Q.QuestionID,
                             Q.Question,
                             options = from QO in context.tbl_Question_Option where      
                                       QO.Status.Equals("A") && QO.QuestionID ==  
                                       Q.QuestionID orderby QO.QuestionOrder select   
                                       new { QO.QuestionOID,  QO.Options },
                             ans = from QO in context.tbl_Question_Option where  
                                   QO.Status.Equals("A") && QO.QuestionID == 
                                   Q.QuestionID && QO.IsCorrect.Equals("Y") select   
                                   QO.QuestionOID,
                         }).ToList().Select(x => new { x.QuestionID, x.Question, obj =   
                          (string.Join(" ,", x.options)).AsEnumerable(),x.ans });
                      var ser = new JavaScriptSerializer();
                      string temp = ser.Serialize(query);



如下。在这里,我已经硬编码了一些东西以降低复杂性。


as below. here I have hardcoded some stuff to reduce complexity.

List<Question> list = new List<Question>();
            list.Add(new Question() { QuestionOID = 1, Options = "Database application" });
            list.Add(new Question() { QuestionOID = 2, Options = "xyz" });
            list.Add(new Question() { QuestionOID = 3, Options = "pqr" });

            var x = new { QuestionID = 1, Question = "MyQuestion", option = list.Select(QO => new {QO.QuestionOID, QO.Options }), ans = "1" };
            var filteredQuery = new { x.QuestionID, x.Question, obj =  x.option, x.ans  };
            var ser = new JavaScriptSerializer();
            string temp = ser.Serialize(filteredQuery);
            Console.WriteLine(temp);



结果如下所示。


And the result will look like below.

{"QuestionID":1,"Question":"MyQuestion","obj":[{"QuestionOID":1,"Options":"Database application"},{"QuestionOID":2,"Options":"xyz"},{"QuestionOID":3,"Options":"pqr"}],"ans":"1"}



谢谢

Srikant


Thanks
Srikant


如果要在客户端将Serialize json字符串转换为JSON对象,可以使用 JSON.Parse( JSONstring)方法。



并且还可以参考以下链接

http://stackoverflow.com/questions/2257117/json-string-to-js-object [ ^ ]
If you want to convert Serialize json string to JSON object on client side you can use JSON.Parse(JSONstring) method.

and also refer below link
http://stackoverflow.com/questions/2257117/json-string-to-js-object[^]


这篇关于如何将以下查询结果转换为JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆