我有这样的json文本:
{ "course_editions": { "2014/SL": [ { "grades": { "course_units_grades": { "159715": { "1": { "value_symbol": "4","exam_session_number": 1,"exam_id": 198172,"value_description": { "en": "good",} } } },"course_grades": {} } },{ "grades": { "course_units_grades": { "159796": { "1": { "value_symbol": "5","exam_id": 198259,"value_description": { "en": "very good",
我想使用Namespace中的JToken.SelectTokens方法:Newtonsoft.Json.Linq
我试过这样的:
string json_response = GetResponse(sign(url_courses)); var courses_tokens = JObject.Parse(json_response).SelectTokens("['course_editions'].['2014/SL'].[*].['grades'].*")
它不起作用.我想在course_unit_grades之后和“1”之前得到这些数字.所以在这个例子中只有:“159715”和“159796”能够一个一个地使用它们
foreach(var lp in courses_tokens) { }
解决方法
这是一种可能的方式:
var jobj = JObject.Parse(json); var coursesTokens = jobj.SelectTokens("course_editions.2014/SL[*].grades.course_units_grades") .Select(o => o.First) //get the first child of `course_units_grades` .Cast<JProperty>() //cast to JProperty .Select(o => o.Name); //get the name of the property foreach (string coursesToken in coursesTokens) { Console.WriteLine(coursesToken); }
159715 159796
json样本:
var json = @"{ 'course_editions': { '2014/SL': [ { 'grades': { 'course_units_grades': { '159715': { '1': { 'value_symbol': '4','exam_session_number': 1,'exam_id': 198172,'value_description': { 'en': 'good' } } } },'course_grades': {} } },{ 'grades': { 'course_units_grades': { '159796': { '1': { 'value_symbol': '5','exam_id': 198259,'value_description': { 'en': 'very good' } } } },'course_grades': {} } } ] } }";