2017-09-05 150 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

它很有魅力,非常感謝 –