На данный вопрос уже ответили:
Есть строка JSON:
{
"ICAO": "EGLC",
"IATA": "LCY",
"name": "London City",
"region": "United Kingdom",
"elevation": 19,
"lat": 51.5052306,
"lon": 0.05517525,
"magneticVariation": -0.7303151023049803,
"timezone": {
"name": "Europe/London",
"offset": 3600
},
"times": {
"sunrise": "2015-08-04T04:28:42.115Z",
"sunset": "2015-08-04T19:45:39.086Z",
"dawn": "2015-08-04T03:48:43.656Z",
"dusk": "2015-08-04T20:25:37.545Z"
},
"runwayCount": 1,
"runways": [
{
"ident": "09",
"width": 97.998687813,
"length": 4930.643052114,
"bearing": 92.89,
"surface": "Concrete",
"thresholdOffset": 322.014436185,
"overrunLength": 164.009186601,
"ends": [
{
"ident": "09",
"lat": 51.50557275,
"lon": 0.0443354
},
{
"ident": "27",
"lat": 51.50488846,
"lon": 0.0660151
}
],
"navaids": [
{
"ident": "ILST",
"type": "LOC-ILS",
"lat": 51.504842,
"lon": 0.067503,
"airport": "EGLC",
"runway": "09",
"frequency": 111150000,
"slope": null,
"bearing": 92.91,
"name": "ILS-cat-I",
"elevation": 19,
"range": 18
},
{
"ident": "ILST",
"type": "GS",
"lat": 51.506011,
"lon": 0.047403,
"airport": "EGLC",
"runway": "09",
"frequency": 111150000,
"slope": 5.05,
"bearing": 92.903,
"name": "GS",
"elevation": 35,
"range": 10
},
{
"ident": "ILST",
"type": "DME",
"lat": 51.505847,
"lon": 0.055411,
"airport": "EGLC",
"runway": "09",
"frequency": 111150000,
"slope": null,
"bearing": null,
"name": "DME-ILS",
"elevation": 48,
"range": 18
}
]
},
{
"ident": "27",
"width": 97.998687813,
"length": 4930.643052114,
"bearing": 272.91,
"surface": "Concrete",
"thresholdOffset": 243.011811393,
"overrunLength": 279.00262509600003,
"ends": [
{
"ident": "27",
"lat": 51.50488846,
"lon": 0.0660151
},
{
"ident": "09",
"lat": 51.50557275,
"lon": 0.0443354
}
],
"navaids": [
{
"ident": "ILSR",
"type": "LOC-ILS",
"lat": 51.505642,
"lon": 0.042125,
"airport": "EGLC",
"runway": "27",
"frequency": 111150000,
"slope": null,
"bearing": 272.892,
"name": "ILS-cat-I",
"elevation": 19,
"range": 18
},
{
"ident": "ILSR",
"type": "GS",
"lat": 51.505514,
"lon": 0.06335,
"airport": "EGLC",
"runway": "27",
"frequency": 111150000,
"slope": 5.5,
"bearing": 272.903,
"name": "GS",
"elevation": 35,
"range": 10
},
{
"ident": "ILSR",
"type": "DME",
"lat": 51.505794,
"lon": 0.054425,
"airport": "EGLC",
"runway": "27",
"frequency": 111150000,
"slope": null,
"bearing": null,
"name": "DME-ILS",
"elevation": 48,
"range": 18
}
]
}
],
"frequencies": [
{
"type": "REC",
"frequency": 136350000,
"name": "City Information"
},
{
"type": "GND",
"frequency": 121820000,
"name": "City Ground"
},
{
"type": "TWR",
"frequency": 118070000,
"name": "City Tower"
},
{
"type": "TWR",
"frequency": 118400000,
"name": "City Tower"
},
{
"type": "APP",
"frequency": 132700000,
"name": "City Approach"
}
],
"weather": {
"METAR": "EGLC 042150Z AUTO 23009KT 200V270 9999 NCD 17/10 Q1016",
"TAF": null
}
}
Как ее преобразовать в объект в C#?
Помещаем наш json в буфер обмена. В Visual Studio идем в меню View - Paste Special - Paste JSON As Classes. Студия распарсит ваш json и сформирует на его основе структуру классов:
public class Rootobject
{
public string ICAO { get; set; }
public string IATA { get; set; }
public string name { get; set; }
public string region { get; set; }
public int elevation { get; set; }
public float lat { get; set; }
... // и т.д.
При необходимости можете подправить полученные классы - изменить типы свойств, например.
В Solution Explorer кликаем правой кнопкой на References, выбираем Manage NuGet Packages... С вкладки Browse подключаем последнюю версию пакета Newtonsoft.Json.
Возвращаемся к коду. Помещаем наш json в строку:
string json = @"...";
Все двойные кавычки нужно экранировать такой же кавычкой, либо заменить на одинарные кавычки (апострофы).
Парсим:
var rootobject = JsonConvert.DeserializeObject<Rootobject>(json);
Всё, теперь можно обращаться к свойствам объекта, например:
Console.WriteLine(rootobject.weather.METAR);
Здесь в документации лежит подобный пример
Оборудование для ресторана: новинки профессиональной кухонной техники
Частный дом престарелых в Киеве: комфорт, забота и профессиональный уход
Можно ли отправить e-mail серверу проверочный запрос, что б подтвердить существование ящика? (Без авторизации)Поддерживают ли mail-сервера такую...
Когда я select делаю в Server я ловлю ошибку в catch ORA-00936: missing expression: