我想知道如何过滤JSON数据。以下是我的数据:
[
{
"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_year
和term
过滤此列表。例如,我可能希望在例如study_year == 2016/2017
和term == 2
时提取结果,以便仅使用该信息来提供JSON数据。
我如何在C#中执行此操作?这是我的C#代码,但不起作用。
var resultObject = localList[].Values < OnResultsList<JObject>().Select(new["study_year].Value<String>() =="2016/2017");
localList
是存储My JSON对象的地方。
答案 0 :(得分:1)
你可以install Json.NET如果你还没有这样做(你的c#代码使用类型JObject
所以你可能已经拥有了),然后将你的JSON加载到LINQ to JSON {{3 }层次结构,然后使用JToken
查询层次结构以过滤和选择数组项:
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();
或者,您可以将Enumerable.Where()
与SelectTokens()
var queryString = string.Format("$[?(@.term == {0} && @.study_year == '{1}')]", term, study_year);
var values = root.SelectTokens(queryString).ToList();
LINQ to JSON层次结构是JSON字符串的内存中,标记化和解析表示。在筛选了JToken
层次结构后,要重新序列化为JSON字符串,您可以执行以下操作:
var filteredJsonString = JsonConvert.SerializeObject(values, Formatting.Indented);
注意:
在第一个查询中,我将t["term"]
投射到int?
而不是int
,以便在令牌"term"
丢失的情况下,{返回{1}},而不是抛出异常。
在第二个查询中,JSONPath查询字符串为:
null
$[?(@.term == 2 && @.study_year == '2016/2017')]
的值必须出现在单引号中,因为JSON中的实际值是字符串,而study_year
的值不能,因为JSON中的实际值是整数。
示例JSONPath query syntax显示两个选项。