Parse json string for search and element (key / value)

I have the following json in timezones.json file:

{ "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 find a specific key, for example. "Atlantic / Canary" and, as a result, they want to return its meaning, i.e. "GMT standard time."

How to do this using Json.Net or any other effective value in C #?

+6
source share
2 answers

Use a JSON parser, such as 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>(); 
+20
source

You want to convert it to an object first and then access it, usually by checking it.

 JObject obj = JObject.Parse(json); string name = (string) obj["Name"]; 
+10
source

All Articles