I know this type of question has probably been asked before, but I when I tried it it didn't quite work. I am new to working with jsons in C#. So I have a JSON file (example from the file is shorter than in real one), from which I want to get values. Values I need are in 'languages' array.
There is a great solution from getting data from the array on the website:
https://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm
But, I have a problem because there is a 'Result' attribute, and I don't know how to go through it. Also there are a lot of attributes in the top of the json ('Headers') Which I don't need but i think I still need to include them in my code?. It would really help if someone could please send me some tips or a solution on how to do it. There will also be an incomplete example of code, which gives errors. Also if any one of you could also reply with maybe a good video on YouTube or website about Json.net except from the official documentation, that would be amazing. Thanks
Json:
{
"StatusCode": 200,
"Headers": {
"X-XSS-Protection": "1; mode=block",
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'none'",
"Cache-Control": "no-store, no-cache",
"Pragma": "no-cache",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains;",
"x-dp-watson-tran-id": "0cf1366a-5bd0-4210-a291-93a30ea0ac62",
"X-Request-ID": "0cf1366a-5bd0-4210-a291-93a30ea0ac62",
"x-global-transaction-id": "0cf1366a-5bd0-4210-a291-93a30ea0ac62",
"Server": "watson-gateway",
"X-EdgeConnect-MidMile-RTT": "5",
"X-EdgeConnect-Origin-MEX-Latency": "92",
"Date": "Wed, 25 Aug 2021 13:21:23 GMT",
"Connection": "keep-alive"
},
"Result": {
"languages": [
{
"language": "af",
"language_name": "Afrikaans",
"native_language_name": "Afrikaans",
"country_code": "ZA",
"words_separated": true,
"direction": "left_to_right",
"supported_as_source": false,
"supported_as_target": false,
"identifiable": true
},
{
"language": "ar",
"language_name": "Arabic",
"native_language_name": "العربية",
"country_code": "AR",
"words_separated": true,
"direction": "right_to_left",
"supported_as_source": true,
"supported_as_target": true,
"identifiable": true
},
{
"language": "az",
"language_name": "Azerbaijani",
"native_language_name": "آذربایجان دیلی",
"country_code": "AZ",
"words_separated": true,
"direction": "right_to_left",
"supported_as_source": false,
"supported_as_target": false,
"identifiable": true
}
]
}
My incomplete code:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace WorkingWithFiles
{
class FullJson
{
public int StatusCode { get; set; }
public Dictionary<string, List<Headers>> Headers { get; set; }
public Dictionary<string, List<Languages>> Result { get; set; }
}
class Headers
{
public string xXssProtection { get; set; }
public string xContentTypeOptions { get; set; }
public string contentTypeOptions { get; set; }
public string cacheControl { get; set; }
public string pragma { get; set; }
public string strictTransportSecurity { get; set; }
public string xDpWatsonTranId { get; set; }
public string xRequestId { get; set; }
public string xGlobalTransactionId { get; set; }
public string server { get; set; }
public string xEdgeConnectMidMileRTT { get; set; }
public string xEdgeConnectOriginMEXLatency { get; set; }
public string date { get; set; }
public string connection { get; set; }
}
class Languages
{
}
class Program
{
static void Main(string[] args)
{
string root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string path = root + @"\SubtitleTranslator\LanguagesList.json";
FullJson fullJson = JsonConvert.DeserializeObject<FullJson>(File.ReadAllText(path));
Console.WriteLine("Status code: " + fullJson.StatusCode);
}
}
}
fullJson.Result.Languages, with result not being a dictionary, but rather a list of Language objects.