我想解析我的json文件。但是,当我从现有的api获取我的json时,我发现了一个问题,我想创建一个函数来解析我的json并存储在列表中
IRestResponse response = ctm(departTime, departure, arrival);
JObject resultJSon = JObject.Parse(response.Content);
Console.WriteLine("Rows:" + resultJSon);
JArray rows = (JArray)(resultJSon["rows"]);
if (rows == null) return ret;
Console.WriteLine("Rows:" + rows);
foreach (var r in rows)
{
Console.WriteLine(r);
JArray cells = (JArray)r["cell"];
Voyage voyage = new Voyage();
voyage.Arrival = new Ville();
voyage.Arrival.Name = arrival;
voyage.Depart = new Ville(0, departure);
voyage.Company = new Company();
voyage.Company.Name = "CTM";
for (var i = 2; i < cells.Count; i++)
{
string tmp = (string)cells[i];
if (i == 2)
{
voyage.DepartTime =
DateTime.Parse(date.ToString("yyyy/MM/dd") + " " + tmp.Split("
".ToCharArray())[0]);
}
else
if (i == 3)
{
//price
voyage.Price = Double.Parse(tmp);
}
else if (i == 4)
{
voyage.Type = tmp;
if (tmp == null || tmp.Equals(""))
{
voyage.Type = "CONFORT";
}
}
else if (i == 5)
{
string[] els = tmp.Split(" ".ToCharArray());
if (els.Length >= 2)
{
if (els[els.Length - 1].Equals("Lendemain"))
{
DateTime tmpD = new DateTime(date.Year,
date.Month, date.Day);
tmpD = tmpD.AddDays(1);
voyage.ArrivalTime =
DateTime.Parse(tmpD.ToString("yyyy/MM/dd") + " " + els[0]);
}
else
{
voyage.ArrivalTime =
DateTime.Parse(date.ToString("yyyy/MM/dd") + " " + els[0]);
}
}
}
}
ret.Add(voyage);
}
我的json文件如下:
{
"page": "1",
"total": 1,
"records": "8",
"rows": [
{
"id": "0",
"cell": [
"0",
"02802902922332019020501",
"00:30",
"245.0",
"CONFORT",
"08:30 "
]
},
{
"id": "1",
"cell": [
"1",
"02207207215022019020501",
"02:30",
"255.0",
"CONFORT PLUS",
"11:30 "
]
},
{
"id": "2",
"cell": [
"2",
"01300200208312019020601",
"08:30",
"250.0",
"CONFORT PLUS",
"17:15 "
]
},
{
"id": "3",
"cell": [
"3",
"02239639609012019020601",
"11:30",
"250.0",
"CONFORT PLUS",
"20:30 "
]
},
{
"id": "4",
"cell": [
"4",
"02200200213002019020601",
"13:00",
"245.0",
"",
"21:30 "
]
},
{
"id": "5",
"cell": [
"5",
"01300200221322019020601",
"21:30",
"250.0",
"CONFORT PLUS",
"05:35 Lendemain"
]
},
{
"id": "6",
"cell": [
"6",
"02702902920312019020601",
"22:30",
"245.0",
"CONFORT",
"06:15 Lendemain"
]
},
{
"id": "7",
"cell": [
"7",
"01300200223032019020601",
"23:00",
"290.0",
"PREMIUM",
"06:15 Lendemain"
]
}
]
}
我的上班时间是航程,这是课程:
public class Voyage
{
public Voyage()
{
}
public Ville Arrival { get; set; }
public Ville Depart { get; set; }
public Company Company { get; set; }
public string Type { get; set; }
public double Price { get; set; }
public DateTime ArrivalTime { get; set; }
public DateTime DepartTime { get; set; }
}
请帮助我找出问题出在哪里
我的问题已解决。问题在这里:
voyage.Price=Double.Parse(tmp);
我无法解析我的字符串。 这是怎么做的:
voyage.Price = double.Parse(tmp, CultureInfo.InvariantCulture);
答案 0 :(得分:0)
我强烈建议使用Newtonsoft进行JSON序列化和反序列化(可以在NuGet包中找到)。使用Newtonsoft,您可以序列化为强类型的对象,反之亦然。在这种情况下,您的代码将看起来像这样:
var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Voyage>(response.Content);
responseObj将包含所有反序列化的信息,而“行”对象将在“单元格”属性中包含更详细的信息。
对于Voyage,班级看起来像这样
public class Row
{
public string id { get; set; }
public List<string> cell { get; set; }
}
public class Voyage
{
public string page { get; set; }
public int total { get; set; }
public string records { get; set; }
public List<Row> rows { get; set; }
}