Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# JToken.SelectTokens Method - what JPath expression?

Tags:

json

c#

linq

I have json text like this:

{
"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_session_number": 1,
            "exam_id": 198259,
            "value_description": {
              "en": "very good",
            }
          }
        }
      },
      "course_grades": {}
    }
  },

I would like to use JToken.SelectTokens Method from Namespace: Newtonsoft.Json.Linq

I've tried like this:

string json_response = GetResponse(sign(url_courses));
var courses_tokens = JObject.Parse(json_response).SelectTokens("['course_editions'].['2014/SL'].[*].['grades'].*")

It doesn't work. I would like to get only these numbers after course_unit_grades and before "1". So in this example only: "159715" and "159796" to be able to use all of them, one by one in

foreach(var lp in courses_tokens) {
}
like image 235
Filip Kowalski Avatar asked Jul 12 '15 11:07

Filip Kowalski


1 Answers

This is one possible way :

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);
}

Dotnetfiddle Demo

given json sample at the bottom, the output is as follow :

159715
159796

json sample :

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_session_number': 1,
                        'exam_id': 198259,
                        'value_description': {
                           'en': 'very good'
                        }
                     }
                  }
               },
               'course_grades': {}
            }
         }
      ]
   }
}";
like image 100
har07 Avatar answered Nov 04 '22 04:11

har07