Json-парсер, есть ли готовые библиотеки - C#
Формулировка задачи:
Здравствуйте, товарищи.
Помогите разобраться с такой задачей:
Есть вот такая штука:
и таких констукций внутри data штук этак 40,
Подскажите каким образом достать из всего этого добра значение ну скажем prices , city , ну никак не получается...
Я пробую использовать JSON.Net, получается только для простеньких конструкций , например так:
но что делать с этой огромной конструкцией не пойму... может быть подскажете другую библиотеку для такой задачки , желательно немного объяснив как это сделать.
Спасибо.
{
"data": [
{ "index": "0",
"serviceClass": "Эконом",
"prices": [3075,70,80],
"eTicketing": true,
"segments": [
{ "indexSegment": "0",
"stopsCount": "0",
"flights": [
{ "index": "0",
"time":
"PT2H",
"minAvailSeats": "4",
"flightLegs": [
{ "flightNo": "138",
"eTicketing": true,
"airplane": "Boeing 737",
"airline": "Кубань",
"airlineCode": "GW",
"from": {
"code": "VKO",
"airport": "Внуково",
"city": "Москва",
"country": "Россия",
"countryCode": "RU",
"terminal": ""},
"to": {
"code": "KRR",
"airport": "Пашковская",
"city": "Краснодар",
"country": "Россия",
"countryCode": "RU",
"terminal": ""},
"fromDate": "2012-01-04",
"fromTime": "08:10:00",
"toDate": "2012-01-04",
"toTime": "10:10:00",
"flightClass": "ECONOMY",
"fareBasis": "SSXOW",
"availSeats": 4,
"classCode": "S"
}],
"stops": [] }] }] }]} private void button1_Click(object sender, EventArgs e)
{
string json = @"{
""index"": ""0"", ""serviceClass"": ""Эконом""
}";
JObject o = JObject.Parse(json);
string index = (string)o["index"];
MessageBox.Show(index);
}Решение задачи: «Json-парсер, есть ли готовые библиотеки»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using System.IO;
using Newtonsoft.Json.Linq;
[JsonObject]
public struct JsonUserObejct
{
[JsonProperty]
public string id
{
get;
set;
}
[JsonProperty]
public Frs[] frs
{
get;
set;
}
[JsonProperty]
public Trps1[] trps
{
get;
set;
}
[JsonProperty]
public int frsCnt
{
get;
set;
}
[JsonProperty]
public Rates rates
{
get;
set;
}
[JsonProperty]
public GdsInfs[] gdsInfs
{
get;
set;
}
*********************PLANES****************************
[JsonProperty]
public Planes planes
{
get;
set;
}
*******************************************************
[JsonProperty]
public Timings timings
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Trps1
{
[JsonProperty]
public string stDt
{
get;
set;
}
[JsonProperty]
public string stTm
{
get;
set;
}
[JsonProperty]
public string endTm
{
get;
set;
}
[JsonProperty]
public string airCmp
{
get;
set;
}
[JsonProperty]
public string fltNm
{
get;
set;
}
[JsonProperty]
public bool conx
{
get;
set;
}
[JsonProperty]
public string stTrm
{
get;
set;
}
[JsonProperty]
public string endTrm
{
get;
set;
}
[JsonProperty]
public string fltTm
{
get;
set;
}
[JsonProperty]
public string jrnTm
{
get;
set;
}
[JsonProperty]
public bool confrm
{
get;
set;
}
[JsonProperty]
public string plane
{
get;
set;
}
[JsonProperty]
public bool eTkAvail
{
get;
set;
}
[JsonProperty]
public string avlSrc
{
get;
set;
}
[JsonProperty]
public int miles
{
get;
set;
}
[JsonProperty]
public int age
{
get;
set;
}
[JsonProperty]
public bool ageAbs
{
get;
set;
}
[JsonProperty]
public int stars
{
get;
set;
}
[JsonProperty]
public FlightInfo flightInfo
{
get;
set;
}
[JsonProperty]
public string key
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct FlightInfo
{
[JsonProperty]
public FirstType ages
{
get;
set;
}
[JsonProperty]
public FirstType delay
{
get;
set;
}
[JsonProperty]
public SecondType delay15
{
get;
set;
}
[JsonProperty]
public SecondType delay30
{
get;
set;
}
[JsonProperty]
public FirstType late
{
get;
set;
}
[JsonProperty]
public FirstType cancelled
{
get;
set;
}
[JsonProperty]
public SecondType seatsmaxPE
{
get;
set;
}
[JsonProperty]
public SecondType seatsminPE
{
get;
set;
}
[JsonProperty]
public FirstType seatPitch
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct FirstType
{
[JsonProperty]
public int stars
{
get;
set;
}
[JsonProperty]
public int value
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct SecondType
{
[JsonProperty]
public int value
{
get;
set;
}
[JsonProperty]
public int stars
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Rates
{
[JsonProperty]
public string USDEUR
{
get;
set;
}
[JsonProperty]
public string USDRUB
{
get;
set;
}
[JsonProperty]
public string USDUAH
{
get;
set;
}
[JsonProperty]
public string EURUSD
{
get;
set;
}
[JsonProperty]
public string EURUAH
{
get;
set;
}
[JsonProperty]
public string RUBUSD
{
get;
set;
}
[JsonProperty]
public string RUBEUR
{
get;
set;
}
[JsonProperty]
public string RUBUAH
{
get;
set;
}
[JsonProperty]
public string UAHUSD
{
get;
set;
}
[JsonProperty]
public string UAHRUB
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct GdsInfs
{
[JsonProperty]
public string areRulesRequestId
{
get;
set;
}
[JsonProperty]
public string hash
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Timings
{
[JsonProperty]
public string start
{
get;
set;
}
[JsonProperty]
public int[] timings
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Frs
{
[JsonProperty]
public int id
{
get;
set;
}
[JsonProperty]
public Dirs[] dirs
{
get;
set;
}
[JsonProperty]
public int gdsInf
{
get;
set;
}
[JsonProperty]
public string lstTkDt
{
get;
set;
}
[JsonProperty]
public PrcInf prcInf
{
get;
set;
}
[JsonProperty]
public int stAvl
{
get;
set;
}
[JsonProperty]
public InfMsgs[] infMsgs
{
get;
set;
}
[JsonProperty]
public string srvCls
{
get;
set;
}
[JsonProperty]
public string sourceTypes
{
get;
set;
}
[JsonProperty]
public int rqId
{
get;
set;
}
[JsonProperty]
public int dirsCnt
{
get;
set;
}
[JsonProperty]
public int fid
{
get;
set;
}
[JsonProperty]
public bool sale
{
get;
set;
}
[JsonProperty]
public string frKey
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Dirs
{
[JsonProperty]
public int id
{
get;
set;
}
[JsonProperty]
public Trps[] trps
{
get;
set;
}
[JsonProperty]
public string jrnTm
{
get;
set;
}
[JsonProperty]
public string bg
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Trps
{
[JsonProperty]
public int id
{
get;
set;
}
[JsonProperty]
public string cls
{
get;
set;
}
[JsonProperty]
public string srvCls
{
get;
set;
}
[JsonProperty]
public int stAvl
{
get;
set;
}
[JsonProperty]
public string avlSrc
{
get;
set;
}
[JsonProperty]
public bool eTkAvail
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct PrcInf
{
[JsonProperty]
public int adtB
{
get;
set;
}
[JsonProperty]
public int adtT
{
get;
set;
}
[JsonProperty]
public string cur
{
get;
set;
}
[JsonProperty]
public int mkup
{
get;
set;
}
[JsonProperty]
public int amt
{
get;
set;
}
[JsonProperty]
public int markupNew
{
get;
set;
}
[JsonProperty]
public int discount
{
get;
set;
}
[JsonProperty]
public int adtM
{
get;
set;
}
[JsonProperty]
public int adtMarkupNew
{
get;
set;
}
[JsonProperty]
public int adtDiscoun
{
get;
set;
}
[JsonProperty]
public int cnnM
{
get;
set;
}
[JsonProperty]
public int cnnMarkupNew
{
get;
set;
}
[JsonProperty]
public int cnnDiscount
{
get;
set;
}
[JsonProperty]
public int infM
{
get;
set;
}
[JsonProperty]
public int infMarkupNew
{
get;
set;
}
[JsonProperty]
public int infDiscount
{
get;
set;
}
[JsonProperty]
public int adtA
{
get;
set;
}
[JsonProperty]
public int cnnA
{
get;
set;
}
[JsonProperty]
public int infA
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct InfMsgs
{
[JsonProperty]
public string tp
{
get;
set;
}
[JsonProperty]
public string cd
{
get;
set;
}
[JsonProperty]
public string msg
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct pmtVrnts
{
[JsonProperty]
public string tag
{
get;
set;
}
[JsonProperty]
public Transactions[] transactions
{
get;
set;
}
[JsonProperty]
public PassengersCount[] passengersCount
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct Transactions
{
[JsonProperty]
public string paySystem
{
get;
set;
}
[JsonProperty]
public string cur
{
get;
set;
}
[JsonProperty]
public int adtA
{
get;
set;
}
[JsonProperty]
public int cnnA
{
get;
set;
}
[JsonProperty]
public int infA
{
get;
set;
}
[JsonProperty]
public int adtC
{
get;
set;
}
[JsonProperty]
public int cnnC
{
get;
set;
}
[JsonProperty]
public int infC
{
get;
set;
}
[JsonProperty]
public int total
{
get;
set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public struct PassengersCount
{
[JsonProperty]
public int adtCount
{
get;
set;
}
[JsonProperty]
public int cnnCount
{
get;
set;
}
[JsonProperty]
public int infCount
{
get;
set;
}
}
namespace _12trip_parser
{
class Program
{
static void Main(string[] args)
{
string jsonFileName = @"c:\json3.json";
string jsonObj = File.ReadAllText(jsonFileName, Encoding.UTF8);
JsonUserObejct jUserObject = JsonConvert.DeserializeObject<JsonUserObejct>(jsonObj);
Console.WriteLine("Какойто там id:" +
(jUserObject.frs[0].id));
Console.WriteLine("Еще какая-то хурмень:" +
(jUserObject.frs[8].dirs[0].trps[0].cls));
Console.WriteLine("Еще какая-то хурмень2:" +
(jUserObject.trps[8].stDt));
Console.ReadKey(true);
}
}
}