c# - 在 Gridview 中显示反序列化 Json 的问题
问题描述
这是我第一次在这里发帖,我已经尝试过搜索,但我找不到任何适合我的建议解决方案。
我正在尝试从 api 调用中获取 Json 响应并将其显示在 Web 网格视图中。目前我的网格只显示 json 的第一部分。见图片。我不确定是因为我的观点不正确还是因为我的反序列化。
我希望它在下面的响应 json 中显示运营商的每个费率:
[
{
“成功”:真,
“rate_response”:[
{
“scac”:“DAFG”,
“服务”:“dayton_ltl”,
“service_human”:“代顿货运”,
“承运人”: “代顿”,“承运人_人类
”: “Dayton”、
“estimated_transit_days”:1、
“cost”:“$145.53”
}、
{
“service”:“FEDEX_EXPRESS_SAVER”、
“service_human”:“FedEx Express Saver”、
“carrier”:“fedex”、
“carrier_human”: “联邦快递包裹”,
“estimated_transit_days”:3,
“成本”:“179.78 美元”
},
],
"rate_response_errors": [
{
"carrier": "BCP Transportation",
"message": "未检测到里程数"
}
],
“pick_ticket_number”:“99999”,
“bol_number”:“4861521889418148”
}
]`
电流控制器
var result = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
return View("GridView", result);
ShipResponse 类:
public class RateResponse
{
[JsonProperty("scac")]
public string scac { get; set; }
[JsonProperty("service")]
public string service { get; set; }
[JsonProperty("service_human")]
public string service_human { get; set; }
[JsonProperty("carrier")]
public string carrier { get; set; }
[JsonProperty("carrier_human")]
public string carrier_human { get; set; }
[JsonProperty("estimated_transit_days")]
public int estimated_transit_days { get; set; }
[JsonProperty("cost")]
public string cost { get; set; }
}
public class RateResponseError
{
[JsonProperty("carrier")]
public string carrier { get; set; }
[JsonProperty("message")]
public string message { get; set; }
}
public class RootObject
{
[JsonProperty("success")]
public bool success { get; set; }
[JsonProperty("rate_response")]
public IList<RateResponse> rate_response { get; set; }
[JsonProperty("rate_response_errors")]
public IList<RateResponseError> rate_response_errors { get; set; }
[JsonProperty("pick_ticket_number")]
public string pick_ticket_number { get; set; }
}
}
GridView.cshtml:
@model List<P21.Rules.Visual.Areas.SwanleapQuote.Models.RootObject>
@{
ViewBag.Title = "GridView";
Layout = "~/Views/Shared/_VisualRuleLayout.cshtml";
}
<div>
<hr />
</div>
@foreach (var item in Model)
{
<div class="form-horizontal">
<form>
<h3>Rates</h3>
@{
var grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 2);
grid.Pager(WebGridPagerModes.NextPrevious);
}
<div>
@grid.GetHtml(htmlAttributes: new
{
id = "grid",
@class = "table table-bordered table-striped table-condensed"
},
emptyRowCellValue: "No Records Found",
headerStyle: "grid-header")
</div>
<button class="btn btn-warning pull-right" type="submit">Return <i class="glyphicon glyphicon-arrow-right"></i></button>
</form>
</div>
我目前的结果只显示:
成功 , rate_response , rate_response_errors , pick_ticket_number
我希望它向我展示:
service_human,estimate_transit_days,成本
解决方案
请检查以下代码:
public class RateResponse
{
public string scac { get; set; }
public string service { get; set; }
public string service_human { get; set; }
public string carrier { get; set; }
public string carrier_human { get; set; }
public int estimated_transit_days { get; set; }
public string cost { get; set; }
}
public class RateResponseError
{
public string carrier { get; set; }
public string message { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public List<RateResponse> rate_response { get; set; }
public List<RateResponseError> rate_response_errors { get; set; }
public string pick_ticket_number { get; set; }
public string bol_number { get; set; }
}
反序列化对象:
var x = JsonConvert.DeserializeObject<RootObject>(jsonString);
json字符串:
[{
"success": true,
"rate_response": [
{
"scac": "DAFG",
"service": "dayton_ltl",
"service_human": "Dayton Freight",
"carrier": "dayton",
"carrier_human": "Dayton",
"estimated_transit_days": 1,
"cost": "$145.53"
},
{
"service": "FEDEX_EXPRESS_SAVER",
"service_human": "FedEx Express Saver",
"carrier": "fedex",
"carrier_human": "Fedex Parcel",
"estimated_transit_days": 3,
"cost": "$179.78"
}],
"rate_response_errors": [
{
"carrier": "BCP Transportation",
"message": "No Mileage Detected"
}
],
"pick_ticket_number": "99999",
"bol_number": "4861521889418148"
}
]
.CSHTML 页面
You can use foreach loop for create table as per your requirement