【问题标题】:Issues showing Deserialized Json in Gridview在 Gridview 中显示反序列化 Json 的问题
【发布时间】:2019-10-02 16:14:55
【问题描述】:

这是我第一次在这里发帖,我已经尝试过搜索,但我找不到任何适合我的建议解决方案。

我正在尝试从 api 调用中获取 Json 响应并将其显示在 Web 网格视图中。目前我的网格只显示 json 的第一部分。见图片。我不确定是因为我的观点不正确还是因为我的反序列化。

New Response

我希望它在下面的响应 json 中显示运营商的每个费率:

[ { "success": true, "rate_response": [ { "scac": "DAFG", "service": "dayton_ltl", "service_human": "代顿货运", "carrier": "dayton", "carrier_human": "代顿", "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": [ {@9876545 "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>

我目前的结果只显示:

Success , rate_response , rate_response_errors , pick_ticket_number

我想让它给我看:

service_human、estimate_transit_days、成本

【问题讨论】:

  • 我看不到代码、图片和 JSON。请将代码和数据以文本形式发布,not as pictures.
  • 对不起,这是我第一次发帖。谢谢你的快速反应。我添加了一些代码和图片。

标签: c# asp.net-mvc model-view-controller model model-view


【解决方案1】:

请检查以下代码:

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);

JsonString:

[{
"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

【讨论】:

  • 我做了你建议的改变(见原帖)。现在我正在从 Json 的第一级取回数据。我从 pick_ticket_number 和网格中的成功获取值。我怎样才能进入下一个级别。当我尝试将模型更改为 RateResponse 而不是 RootObject 时,我得到了我正在寻找的列标题(Scac、Service、Service_Human、Estimated_Ship_days 等),但它们都是空白的。
  • 您可以通过以下方式获得预期值:@foreach (var item in Model) { var x = item.rate_response; foreach(var item2 in x) { string scac = item2.scac;字符串 service_human = item2.service_human; } }
  • 感谢您的帮助,图沙尔。那行得通。在我发布一个新问题之前,我忘记了这个问题。
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 2021-05-28
  • 1970-01-01
相关资源
最近更新 更多