如何将多层json数据绑定到asp.net中的中继器或将json数据转换为数据表
本文关键字:json 中继器 数据 转换 数据表 asp 数据绑定 net | 更新日期: 2023-09-27 17:50:46
我想将Json数据绑定到中继器我知道只有一个过程是将Json数据转换为数据表,然后绑定数据,但在这里我接收多层Json数据,我不知道如何将它们转换为数据表
输入json数据:
{"apiAvailableBuses":
[{"droppingPoints":null,"availableSeats":40,"partialCancellationAllowed":false,"arrivalTime":"01:00 AM","cancellationPolicy":"[{'"cutoffTime'":'"1'",'"refundInPercentage'":'"10'"},{'"cutoffTime'":'"2'",'"refundInPercentage'":'"50'"},{'"cutoffTime'":'"4'",'"refundInPercentage'":'"90'"}]","boardingPoints":[{"time":"07:40PM","location":"K.P.H.B,Beside R.S Brothers","id":"2238"}],"operatorName":"Apple I Bus","departureTime":"8:00 PM","mTicketAllowed":false,"idProofRequired":false,"serviceId":"6686","fare":"1000","busType":"Hi-Tech A/c","routeScheduleId":"6686","commPCT":9.0,"operatorId":203,"inventoryType":0},
{
"droppingPoints":null,"availableSeats":41,"partialCancellationAllowed":false,"arrivalTime":"06:00 AM","cancellationPolicy":"[{'"cutoffTime'":'"1'",'"refundInPercentage'":'"10'"},{'"cutoffTime'":'"2'",'"refundInPercentage'":'"50'"},{'"cutoffTime'":'"4'",'"refundInPercentage'":'"90'"}]","boardingPoints":[{"time":"08:00PM","location":"Punjagutta,","id":"2241"}],"operatorName":"Royalcoach Travels","departureTime":"8:00 PM","mTicketAllowed":false,"idProofRequired":false,"serviceId":"6736","fare":"800","busType":"VOLVO","routeScheduleId":"6736","commPCT":9.0,"operatorId":243,"inventoryType":0}
我正在尝试通过
将其转换为数据表 public void getavailablebuses()
{
string url = string.Format(HttpContext.Current.Server.MapPath("files/getavailablebuses.json"));
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
var result = JsonConvert.DeserializeObject<RootObject>(json);
string mm = JObject.Parse(json).SelectToken("apiAvailableBuses").ToString();
// var boardingpoint = JObject.Parse(mm).SelectToken("boardingPoints").ToString();
var Availablebuses = JObject.Parse(json).SelectToken("apiAvailableBuses").ToString();
DataTable dt = (DataTable)JsonConvert.DeserializeObject(Availablebuses, (typeof(DataTable)));
}
public class apiresult
{
public string message { get; set; }
public string success { get; set; }
}
public class RootObject
{
public apiresult apiStatus;
public List<apiAvailableBuses> apiAvailableBuses{ get; set; }
// public string apiAvailableBuses { get; set; }
}
public class apiAvailableBuses
{
public string serviceId { get; set; }
public string fare { get; set; }
public string busType { get; set; }
public string departureTime { get; set; }
public string operatorName { get; set; }
public string cancellationPolicy { get; set; }
public List<boardingpoints> boardingpoints { get; set; }
public string droppingPoints { get; set; }
public string inventoryType { get; set; }
public string routeScheduleId { get; set; }
public int availableSeats { get; set; }
public string arrivalTime { get; set; }
public Boolean idProofRequired { get; set; }
public Boolean partialCancellationAllowed { get; set; }
public int operatorId { get; set; }
public double commPCT { get; set; }
public string mTicketAllowed { get; set; }
}
public class boardingpoints
{
public string location { get; set; }
public string id { get; set; }
public string time { get; set; }
}
public class cancellationPolicy
{
public string cutoffTime { get; set; }
public string refundInPercentage { get; set; }
}
在数据表中,我无法获得登机点,落点和取消策略如果我将取消策略加载为列表或JObject,我会得到错误这里我将取消策略加载为字符串
但是我无法载入登机点和降落点。请帮助这个我抓耳挠腮从两天。提前感谢
"我只知道一种方法将数据绑定到中继器,即数据表。"所以这是学习其他方法的绝佳机会,你不觉得吗?
你为什么不和JsonConvert.DeserializeObject<RootObject>(json);
的result
一起工作?这是一个RootObject
,它有一种叫做apiAvailableBuses
的特性,它似乎正是你需要绑定到repeater
上的东西,不是吗?
顺便说一下,稍微回顾一下代码:
-
apiresult
和apiAvailableBuses
违反了Microsoft的WRT类名规则:它们应该使用PascalCase。apiresult
的属性也一样,例如message
和success
。apiAvailableBuses
的属性也一样 -
RootObject
有一个公共字段:apiStatus
。这可能需要一个带有getter/setter的属性 -
此外,
apiAvailableBuses
是复数,这是不正确的,因为其中的数据只有一个总线。boardingpoints
相同:类包含单点数据,而不是多个。 -
保持一致:如果你使用
string
,那么也使用bool
,而不是Boolean
。