Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse json string to find and element (key / value) [duplicate]

I have following json in a file timezones.json:

  { "Atlantic/Canary": "GMT Standard Time", "Europe/Lisbon": "GMT Standard Time",  "Antarctica/Mawson": "West Asia Standard Time", "Etc/GMT+3": "SA Eastern Standard Time",  "Etc/GMT+2": "UTC-02", "Etc/GMT+1": "Cape Verde Standard Time", "Etc/GMT+7": "US Mountain  Standard Time", "Etc/GMT+6": "Central America Standard Time", "Etc/GMT+5": "SA Pacific  Standard Time", "Etc/GMT+4": "SA Western Standard Time", "Pacific/Wallis": "UTC+12",  "Europe/Skopje": "Central European Standard Time", "America/Coral_Harbour": "SA Pacific  Standard Time", "Asia/Dhaka": "Bangladesh Standard Time", "America/St_Lucia": "SA Western  Standard Time", "Asia/Kashgar": "China Standard Time", "America/Phoenix": "US Mountain  Standard Time", "Asia/Kuwait": "Arab Standard Time" } 

I want to search for a particular key e.g. "Atlantic/Canary" from it and as a result want its value back i.e. "GMT Standard Time".

How can I do this using Json.Net or any other efficient mean in C#?

like image 985
theGeekster Avatar asked Feb 10 '14 13:02

theGeekster


1 Answers

Use a JSON parser, like JSON.NET

string json = "{ \"Atlantic/Canary\": \"GMT Standard Time\", \"Europe/Lisbon\": \"GMT Standard Time\", \"Antarctica/Mawson\": \"West Asia Standard Time\", \"Etc/GMT+3\": \"SA Eastern Standard Time\", \"Etc/GMT+2\": \"UTC-02\", \"Etc/GMT+1\": \"Cape Verde Standard Time\", \"Etc/GMT+7\": \"US Mountain Standard Time\", \"Etc/GMT+6\": \"Central America Standard Time\", \"Etc/GMT+5\": \"SA Pacific Standard Time\", \"Etc/GMT+4\": \"SA Western Standard Time\", \"Pacific/Wallis\": \"UTC+12\", \"Europe/Skopje\": \"Central European Standard Time\", \"America/Coral_Harbour\": \"SA Pacific Standard Time\", \"Asia/Dhaka\": \"Bangladesh Standard Time\", \"America/St_Lucia\": \"SA Western Standard Time\", \"Asia/Kashgar\": \"China Standard Time\", \"America/Phoenix\": \"US Mountain Standard Time\", \"Asia/Kuwait\": \"Arab Standard Time\" }"; var data = (JObject)JsonConvert.DeserializeObject(json); string timeZone = data["Atlantic/Canary"].Value<string>(); 
like image 118
Thomas Levesque Avatar answered Sep 19 '22 14:09

Thomas Levesque