我无法在下面看到此JSON格式的数据。虽然我已经/设置但我不知道为什么或如何以这种格式显示数据!
所需格式:
{
"cinemaSF" : [
{
"customer_id" : "customer0001",
"ticket" : {
"ticket_id" : "1",
"round" : {
"idticket" : "2015-01-01 15:44:40",
"pieces" : "13.00",
"rate" : "100.00"
},
"down" : {
"idticket" : "2015-01-01 21:04:40",
"pieces" : "13.00",
"rate" : "100.00"
}
},
"dur_info" : {
"start" : "102.34",
"end" : "90.34",
"climax" : "120.50"
},
"cine_event" : {
"score" : "80.5",
"climax_event" : [
{
"event_id" : "0",
"idticket" : "2015-01-01 15:44:40",
"pieces" : "13.00",
"rate" : "100.00"
},
{
"event_id" : "0",
"idticket" : "2015-01-01 15:44:40",
"pieces" : "13.00",
"rate" : "100.00"
}
}
}
]
}
相反,我得到这种格式:
public class cinemaSF
{
public string customer_id { get; set; }
public List<ticket> ticket_cinemaSF { get; set; }
public List<dur_info> dur_info_cinemaSF { get; set; }
public List<driving_event> driving_event_cinemaSF { get; set; }
}
public class ticket
{
public int ticket_id { get; set; }
public List<pieces> pieces_cinemaSF { get; set; }
public List<rate> rate_cinemaSF { get; set; }
}
public class round
{
public string idticket { get; set; }
public string pieces { get; set; }
public string rate { get; set; }
}
public class down
{
public string idticket { get; set; }
public string pieces { get; set; }
public string rate { get; set; }
}
public class dur_info
{
public string start { get; set; }
public string end { get; set; }
public string climax { get; set; }
}
public class cine_event
{
public string score { get; set; }
public List<climax_event> climax_event_cinemaSF { get; set; }
}
public class climax_event
{
public string event_id { get; set; }
public string idticket { get; set; }
public string pieces { get; set; }
public string rate { get; set; }
}
我使用以下代码检索数据:
string url = "http://XXXXXXX.php";
var client_cinemaSF = new RestClient(url);
var request_cinemaSF = new RestRequest(Method.GET);
IRestResponse response_cinemaSF = client_cinemaSF.Execute(request_cinemaSF);
var content_cinemaSF = response_cinemaSF.Content;
cinemaSF root_cinemaSF = new cinemaSF();
cinemaSF deserializePlayer_cinemaSF = JsonConvert.DeserializeObject<cinemaSF>(content_cinemaSF.ToString());
Console.WriteLine(deserializePlayer_cinemaSF.trip_cinemaSF[0]);
为什么我不能展示deserializePlayer_cinemaSF.trip_cinemaSF [0]
答案 0 :(得分:1)
我发现的问题很少
JSON格式不正确,应该看起来像这样
{
"cinemaSF": [
{
"customer_id": "customer0001",
"ticket": {
"ticket_id": "1",
"round": {
"idticket": "2015-01-01 15:44:40",
"pieces": "13.00",
"rate": "100.00"
},
"down": {
"idticket": "2015-01-01 21:04:40",
"pieces": "13.00",
"rate": "100.00"
}
},
"dur_info": {
"start": "102.34",
"end": "90.34",
"climax": "120.50"
},
"cine_event": {
"score": "80.5",
"climax_event": [
{
"event_id": "0",
"idticket": "2015-01-01 15:44:40",
"pieces": "13.00",
"rate": "100.00"
},
{
"event_id": "0",
"idticket": "2015-01-01 15:44:40",
"pieces": "13.00",
"rate": "100.00"
}
]
}
}
]
}
接下来我们需要了解其中元素的层次结构
您还需要一个用于JSON的RootElement来对其进行Desearlize,因此它应该看起来像这样
public class RootJson
{
[JsonProperty("cinemaSF")]
public cinemaSF csf { get; set; }
}
public class cinemaSF
{
[JsonProperty("customer_id")]
public string customer_id { get; set; }
[JsonProperty("ticket")]
public List<ticket> ticket_cinemaSF { get; set; }
[JsonProperty("dur_info")]
public List<dur_info> dur_info_cinemaSF { get; set; }
[JsonProperty("cine_event")]
public List<driving_event> driving_event_cinemaSF { get; set; }
}
现在到了最后一部分
班级ticket
必须
[JsonProperty("ticket_id")]
[JsonProperty("round")]
[JsonProperty("down")]
和Round
以及Down
可以
public List<pieces>
public List<rate>
然后最后你可以去
RootJson deserializePlayer_cinemaSF = JsonConvert.DeserializeObject<RootJson>(content_cinemaSF.ToString());