Home > Net >  Text Extraction from JSON using C# in Window Forms
Text Extraction from JSON using C# in Window Forms

Time:03-01

I am working with OXFORD DICTIONARY API which gives meaning to a word. It returns the result in JSON format and hence I want to extract only the meaning of the word from the JSON. I am using the C# NEWTONSOFT package but am unable to get the output. The code and output screenshot I attached below any help is highly appreciated.

using System;
using Newtonsoft.Json;
using System.IO;
using System.Text.Json;
public class Program
{
    public static void Main()
    {
        string word_id = " ";
        Console.WriteLine("Enter a word : ");
        word_id= Console.ReadLine();
        const string lang_code = "en-gb";
        const string fields = "definitions";
        const string strictMatch = "false";
        string WEBSERVICE_URL = "https://od-api.oxforddictionaries.com:443/api/v2/entries/"   lang_code   '/'   word_id   "?fields="   fields   "&strictMatch="   strictMatch;
        try
        {
#pragma warning disable SYSLIB0014 // Type or member is obsolete
            var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
#pragma warning restore SYSLIB0014 // Type or member is obsolete
            if (webRequest != null)
            {
                webRequest.Method = "GET";
                webRequest.Timeout = 12000;
                webRequest.ContentType = "application/json";
                webRequest.Headers.Add("app_id", "xxxxxxx");
                webRequest.Headers.Add("app_key", "xxxxxxxxxxxxxxxxxxxxxxxxxx");

                using System.IO.Stream s = webRequest.GetResponse().GetResponseStream();
                using System.IO.StreamReader sr = new System.IO.StreamReader(s);
                var jsonresponse = sr.ReadToEnd();
                var jdata = JsonConvert.DeserializeObject<details>(jsonresponse);
                Console.WriteLine("Meaning : ", arg0: jdata.results.lexicalEntries.entries.senses.definitions);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        
    }
}

public class details
{
    public string id { get; set; }
    public Metadata metadata { get; set; }
    public results results { get; set; }
    public string word { get; set; }

}

public class Metadata
{
    public string? operation{ get; set; }
    public string? provider { get; set; }
    public string? schema{ get; set; }
}

public class results
{
    public string? id { get; set; }
    public string? language { get; set; }
    public lexicalEntries lexicalEntries { get; set; }
    public string? type { get; set; }
    public string? word { get; set; }
}

public class lexicalEntries
{
    public entries entries { get; set; }
}

public class entries
{
    public senses senses { get; set; }
}

public class senses
{
    public string definitions { get; set; }
    public string id { get; set; }
}


enter image description here

CodePudding user response:

I prefer using dynamic and ExpandoObject can help you a lot

using System.Dynamic;
dynamic ? obj = JsonConvert.DeserializeObject<ExpandoObject>(yourJSON);

the obj has exact properties as same as your JSON at the same time it does not need to create a model from any JSON

  • Related