2017-09-05 55 views
1

我想知道如何过滤JSON数据。下面是我的数据:Json数组列表使用C#中的值过滤对象#

[ 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L001", 
     "subjectname": "Mathematics", 
     "paper": 1, 
     "markscored": 48, 
     "grade": "E", 
     "tr_comments": "Emmanuel can do better with improved participation in class, more practice and proper usage of his time especially during prep time.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L001", 
     "subjectname": "Mathematics", 
     "paper": 1, 
     "markscored": 45, 
     "grade": "E", 
     "tr_comments": "Emmanuel's behavior has improved and has registered a good class attendance. This score is low but he is capable of making it better. This subject requires practice. He is therefore advised to engage in constant revision and practice and doing his assignments independently in addition to meeting deadlines for assignments.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L001", 
     "subjectname": "Mathematics", 
     "paper": 1, 
     "markscored": 50, 
     "grade": "D", 
     "tr_comments": "Emmanuel, you have the potential to do better than this but your effort is still minimal. The subject has several concepts which one should understand , master and should be able to apply the knowledge in algebra, number, geometry handling data and problem solving. It therefore calls for daily continuous practice if you are to perform well.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L002", 
     "subjectname": "English", 
     "paper": 1, 
     "markscored": 45, 
     "grade": "E", 
     "tr_comments": "Jessy,this is below average,you need to settle down for hard work and improve on this grade.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L002", 
     "subjectname": "English", 
     "paper": 1, 
     "markscored": 28, 
     "grade": "G", 
     "tr_comments": "Kisakye has a number of challenges including grammar, reading comprehension, spellings and written expression. He is encouraged to give extra time for more lessons and to take better care of his property.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L002", 
     "subjectname": "English", 
     "paper": 1, 
     "markscored": 36, 
     "grade": "F", 
     "tr_comments": "Emmanuel is capable of performing better than this but he still has challenges right from paying attention in class to completing given assignments, which denies him the much needed practice to improve his reading comprehension and writing.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L003", 
     "subjectname": "Combined Science", 
     "paper": 1, 
     "markscored": 68, 
     "grade": "C", 
     "tr_comments": "Kimbugwe is interested in the subject and this is reflected in the way he asks questions during our lessons. He has a lot of scientific information in his head but he finds a challenge putting it together. With guidance he will get there. Keep up the participation Jesse. ", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L005", 
     "subjectname": "Environmental Science", 
     "paper": 1, 
     "markscored": 53, 
     "grade": "D", 
     "tr_comments": "Emmanuel shows a lot of interest in his studies.He actively contributes during class discussion...this explains his performance.keep it up!", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L005", 
     "subjectname": "Environmental Science", 
     "paper": 1, 
     "markscored": 30, 
     "grade": "F", 
     "tr_comments": "Kimbugwe has to improve on his revision skill and adequately prepare for the exam so as to yield better marks", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L006", 
     "subjectname": "Information Technology", 
     "paper": 1, 
     "markscored": 65, 
     "grade": "C", 
     "tr_comments": "Emma's grade is fair, but he rushed in the end of term exam. The grade was really below his potential so he sat again and got this better grade. He should always prepare himself well and do his papers without rushing.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L006", 
     "subjectname": "Information Technology", 
     "paper": 1, 
     "markscored": 48, 
     "grade": "E", 
     "tr_comments": "Emmanuel needs to focus more. He had to re-do this test to improve. He also needs to always keep his books safer.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L006", 
     "subjectname": "Information Technology", 
     "paper": 1, 
     "markscored": 76, 
     "grade": "B", 
     "tr_comments": "Emma has not had a book for a considerable part of the term. This slightly affected revision. He also needs to be more attentive during lessons. The on and off episodes of his should really stop.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L008", 
     "subjectname": "Art & Design", 
     "paper": 1, 
     "markscored": 90, 
     "grade": "A*", 
     "tr_comments": "This is an excellent performance. We encourage you to continue with the effort and take more time learning the different terminologies used in design technology. Also continue trying out new ideas, experimenting with paints and other colours.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L008", 
     "subjectname": "Art & Design", 
     "paper": 1, 
     "markscored": 70, 
     "grade": "B", 
     "tr_comments": "Emmanuel has shown some progress in his performance. He is an inquisitive learner who enjoys analyzing situations and making new discoveries in art. He is a self-motivated and hard working. I encourage him to maintain the progress and even do consultations so as to aspire for excellent results\r\n", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L008", 
     "subjectname": "Art & Design", 
     "paper": 1, 
     "markscored": 70, 
     "grade": "B", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L008", 
     "subjectname": "Art & Design", 
     "paper": 1, 
     "markscored": 55, 
     "grade": "D", 
     "tr_comments": "Emmanuel is a learner with great energies who requires plenty of attention in form of follow ups. He is encouraged to be self motivated and stay focused an the task at hand.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L009", 
     "subjectname": "Business & Enterprise", 
     "paper": 1, 
     "markscored": 40, 
     "grade": "E", 
     "tr_comments": "Emmanuel's performance is below average. He is capable of performing better than this only that he is not settled, doesn't listen and follow directions. He is slow at accomplishing assignments and is also easily destructed by friends. He is advised to improve on his work habits if he is to perform better.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L009", 
     "subjectname": "Business & Enterprise", 
     "paper": 1, 
     "markscored": 51, 
     "grade": "D", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L009", 
     "subjectname": "Business & Enterprise", 
     "paper": 1, 
     "markscored": 55, 
     "grade": "D", 
     "tr_comments": "Emmanuel, you are trying to make an improvement on your performance. This is promising However, more efforts need to be committed to your studies first if you are to perform better than this.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L010", 
     "subjectname": "French", 
     "paper": 1, 
     "markscored": 52, 
     "grade": "D", 
     "tr_comments": "Emmanuel is not familiar with enough grammar at this level, though he has absorbed new material effectively. His knowledge and understanding of different structures needs to be increased. However, he is able to make use of what he knows in productive tasks.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L010", 
     "subjectname": "French", 
     "paper": 1, 
     "markscored": 28, 
     "grade": "G", 
     "tr_comments": "Emmanuel needs to consult his teachings to catch up on work misses.He seems to have missed out on a lot of work.Otherwise,he has all the potential to perform better in french", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L010", 
     "subjectname": "French", 
     "paper": 1, 
     "markscored": 0, 
     "grade": "U", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L010", 
     "subjectname": "French", 
     "paper": 1, 
     "markscored": 41, 
     "grade": "E", 
     "tr_comments": "Emma could do better really! He has the potential but he lacks consistent concentration and focus. He comes out as a serious student in one lesson, and and a very playful and distractive student in the other. Inconsistency and playfulness. He likes making fun in class.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L011", 
     "subjectname": "Health & Physical Education", 
     "paper": 1, 
     "markscored": 87, 
     "grade": "A", 
     "tr_comments": "Jessy has greatly improved on his performance this term: He always completes his assignments and classwork in time. However he some times comes late in class because of his love for computer games. We have talked about this and he promises to change for the better.", 
     "studyclass": 7, 
     "term": 3, 
     "study_year": "2015/2016" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L011", 
     "subjectname": "Health & Physical Education", 
     "paper": 1, 
     "markscored": 77, 
     "grade": "B", 
     "tr_comments": "Jessy Emmanuel enjoys and always attends the H&PE lessons most especially the practicals. He has improved on his pace when writing his notes. He rarely puts on his proper P.E attire; he is encouraged to work on his weak areas like revising his notes for a better grade in next term. ", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L011", 
     "subjectname": "Health & Physical Education", 
     "paper": 1, 
     "markscored": 56, 
     "grade": "D", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L011", 
     "subjectname": "Health & Physical Education", 
     "paper": 1, 
     "markscored": 66, 
     "grade": "C", 
     "tr_comments": "Jessy has slightly declined in his performance this term: He always completes his assignments and classwork in time. However in practicals, he rarely has an appropriate sports attire and this affects his practicals. He is encouraged to get an appropriate sports attire.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L012", 
     "subjectname": "Combined Science (Biology)", 
     "paper": 1, 
     "markscored": 58, 
     "grade": "D", 
     "tr_comments": "A fair attempt Emma, but you could have done better if you settled, and did more personal reading and sturdy.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L012", 
     "subjectname": "Combined Science (Biology)", 
     "paper": 1, 
     "markscored": 88, 
     "grade": "A", 
     "tr_comments": "-Emmanuel has improved in the subject.But we encourage him to remain focused and avoid those habits that can make him decline in future.\n", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L013", 
     "subjectname": "Combined Science (Chemistry)", 
     "paper": 1, 
     "markscored": 63, 
     "grade": "C", 
     "tr_comments": "This score is fair and he has potential to improve it. He should be encouraged to start reading hard before the work accumulates. He is advised do extensive personal reading to improve on this grade. I have no doubt he will make it if he does this plus utilizing the teachers.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L013", 
     "subjectname": "Combined Science (Chemistry)", 
     "paper": 1, 
     "markscored": 70, 
     "grade": "B", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L013", 
     "subjectname": "Combined Science (Chemistry)", 
     "paper": 1, 
     "markscored": 73, 
     "grade": "B", 
     "tr_comments": "-Emmanuel has improved on his attitude towards studies.We encourage him to keep on track for better performance in future.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L014", 
     "subjectname": "Combined Science (Physics)", 
     "paper": 1, 
     "markscored": 70, 
     "grade": "B", 
     "tr_comments": "Encouraging performance Emmanuel. Put in more effort for a much better performance. Keep consulting the teacher for effective understanding of the subject.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L014", 
     "subjectname": "Combined Science (Physics)", 
     "paper": 1, 
     "markscored": 70, 
     "grade": "B", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L014", 
     "subjectname": "Combined Science (Physics)", 
     "paper": 1, 
     "markscored": 60, 
     "grade": "C", 
     "tr_comments": "A noticeable improvement has been registered by Kimbugwe as a result of his being more active in class as well as constant teacher supervision.He is advised to do more practice so as to perform much better as he goes to y9 and also learn to work independently.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L015", 
     "subjectname": "Personal, Social & Health Education", 
     "paper": 1, 
     "markscored": 55, 
     "grade": "D", 
     "tr_comments": "Emma shows interest in the subject and endeavors to take his notes however he is advised to do more listening than talking during the lesson to attain better a grade.", 
     "studyclass": 8, 
     "term": 1, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L015", 
     "subjectname": "Personal, Social & Health Education", 
     "paper": 1, 
     "markscored": 48, 
     "grade": "E", 
     "tr_comments": "-", 
     "studyclass": 8, 
     "term": 2, 
     "study_year": "2016/2017" 
    }, 
    { 
     "admno": "VCN-2015-0005", 
     "subcode": "L015", 
     "subjectname": "Personal, Social & Health Education", 
     "paper": 1, 
     "markscored": 68, 
     "grade": "C", 
     "tr_comments": "Emma shows interest in the subject and endeavors to take his notes however he is advised to do more personal reading and utilize his preps to attain a better grade.", 
     "studyclass": 8, 
     "term": 3, 
     "study_year": "2016/2017" 
    } 
] 

我想study_yearterm过滤名单。例如,我可能想要在例如study_year == 2016/2017term == 2时提取结果,以便仅将JSON数据与该信息相关联。

我该如何在C#中做到这一点?这是我的C#代码,但不起作用。

var resultObject = localList[].Values < OnResultsList<JObject>().Select(new["study_year].Value<String>() =="2016/2017"); 

localList是我的JSON对象存储的地方。

+1

你尝试过什么吗? –

+0

https://stackoverflow.com/questions/12676746/parse-json-string-in-c-sharp http://www.c-sharpcorner.com/article/working-with-json-string-in-C -Sharp/ –

+0

yeaah我做了,但仍然无法正常工作,让我更新我的帖子 –

回答

1

你可以install Json.NET如果你还没有这样做的话(你的C#代码使用的类型JObject所以也许你已经有了),然后加载您的JSON为LINQ to JSONJToken层次,然后使用Enumerable.Where()进行过滤和选择阵列查询层次项目:

var root = JToken.Parse(jsonString); 

var study_year = "2016/2017"; 
var term = 2; 
var values = root.Where(t => (int?)t["term"] == term && (string)t["study_year"] == study_year).ToList(); 

或者,你可以使用SelectTokens()JSONPath query syntax

var queryString = string.Format("$[?(@.term == {0} && @.study_year == '{1}')]", term, study_year); 
var values = root.SelectTokens(queryString).ToList(); 

一个LIN Q到JSON层次结构是JSON字符串的内存中,标记和解析表示形式。已经过滤出的JToken层次结构后,重新序列化到JSON字符串,你可以这样做:

var filteredJsonString = JsonConvert.SerializeObject(values, Formatting.Indented); 

注:

  • 在第一个查询,我投t["term"]int?而不是int,以便在缺少令牌"term"的情况下返回null,而不是抛出异常。

  • 在第二个查询中,JSONPath查询字符串:

    $[?(@.term == 2 && @.study_year == '2016/2017')] 
    

    study_year值必须出现在单引号,因为在JSON的实际值是字符串,而term值不能因为JSON中的实际值是整数。

样本fiddle显示两个选项。

+0

Thanks @dbc for the answer,but does var'values = root.Where(t =>(int?)t [“term”] == term &&(string)t [“study_year”== == study_year).ToList ();',有一个排序格式字符串JSON?或者,因为我希望它在进行排序后仍然处于JSON格式。 –

+0

@IdrisStack - 如果你问如何将过滤的JToken列表重新转换为JSON字符串,请执行'var filteredJsonString = JsonConvert.SerializeObject(values,Formatting.Indented);'。我编辑了我的问题以包含此内容。如果不是,我在理解你的英语时遇到了一些麻烦,那么你可以编辑你的问题来表明你想完成什么?你目前的代码甚至没有编译。一个[mcve]将是理想的。另见[问]。 – dbc

+0

它很有魅力,非常感谢 –