0

When I get http response it looks like this:

{
"course_editions": {
"2014/SL": [
  {
    "course_id": "06-DEGZLI0",
    "term_id": "2014/SL",
    "course_name": {
      "en": "Preparation for bachelor exam",
    }
  },
  {
    "course_id": "06-DPRALW0",
    "term_id": "2014/SL",
    "course_name": {
      "en": "Work experience",
    }
  },
  {

I would like to be able to extract course title only, f.e.:

Work experience
Preparation for bachelor exam

I've tried this:

string probably_json = GetResponse(url_courses);
object obj = JsonConvert.DeserializeObject(probably_json);
        using (StringReader reader = new StringReader(obj.ToString().Replace("\\t", "    ").Replace("\\n", "\n")))
            {
                string line;
                int lineNo = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Contains("en"))
                    {
                        string output = line.Substring(0, line.Length-1);
                        Console.WriteLine(output);
                    }
                    ++lineNo;
                }
            } // End Using StreamReader

But that's all I've got:

"en": "Preparation for bachelor exam"          "en": "Work experience"

what am I supposed to do, to get course title only ?

Filip Kowalski
  • 144
  • 1
  • 3
  • 15
  • Why can't you go through properties returned by DeserializeObject? – Alexei Levenkov Jul 02 '15 at 22:19
  • See this answer http://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net , it explains how to use JsonConvert with a dynamic object. You don't need to use StringReader, just call obj.course_name.en – Roman Mik Jul 02 '15 at 22:30

1 Answers1

2

If you are using json.net anyways, make it do some work, don't parse yourself:

var result = JObject
    .Parse(probably_json)
    .SelectTokens("['course_editions'].['2014/SL'].[*].['course_name'].['en']");
Nikolay
  • 10,752
  • 2
  • 23
  • 51