使用json对象

时间:2018-04-17 17:48:54

标签: asp.net json asp.net-mvc

最近我想出了如何在前一个问题中正确序列化json字符串,但我现在很难在视图上显示结果。我修复了我的模型和控制器,但视图似乎是现在的问题。这是我收到的错误。

  

编译错误   描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

     

编译器错误消息:CS1061:'IEnumerable'不包含'sWorkOrderNumber'的定义,并且没有扩展方法'sWorkOrderNumber'可以找到接受类型'IEnumerable'的第一个参数(你是否缺少using指令或程序集)引用?)

     

来源错误:

     

第4行:@foreach(模型中的var顺序)

     

第5行:{

     

第6行:@ Model.sWorkOrderNumber

     

第7行:}

我的控制器代码如下。

string url = @"URL of webservice here";

var returnValue = new WebClient().DownloadString(url);

        List<Order> shipments = JsonConvert.DeserializeObject<List<Order>>(returnValue);

        return View(shipments.ToList());

以下是我的观点。

@model IEnumerable<Project.Models.Order>

@foreach (var order in Model)
{
    @Model.sWorkOrderNumber
}

这是返回的JSON。

[{"ShipmentTrackingNUmbers":[{"sTrackingNumber":"Redacted"}],"dtDateShipped":"5/12/2017 12:00:00 AM","dSPRatedWeight":"41.0000","nMBTotalBoxes":"1","sShipToAddressLine2":"Redacted","sShipToCountry":"U.S.A.","sShipToZip":"Redacted","sShipToState":"Redacted","sShipToCity":"Redacted","sShipToAddressLine1":"Redacted","sShipToName":"Redacted","sServiceLevel":"GND","sInvoiceNumber":"","dtInvoiceDate":"","mInvoiceAmount":"","dtDateReceived":"5/8/2017 3:16:25 PM","sStudioCategory2":"","sStudioCategory1":"","sOrderSource":"Redacted","sCustomerComments":"Redacted","sCustomerOrderName":"Redacted","sWorkOrderNumber":"Redacted","sCustomerNumber":"Redacted"},{"ShipmentTrackingNUmbers":[{"sTrackingNumber":"Redacted"}],"dtDateShipped":"5/10/2016 12:00:00 AM","dSPRatedWeight":"3.0000","nMBTotalBoxes":"1","sShipToAddressLine2":"","sShipToCountry":"U.S.A.","sShipToZip":"Redacted","sShipToState":"Redacted","sShipToCity":"Redacted","sShipToAddressLine1":"Redacted","sShipToName":"Redacted","sServiceLevel":"GND","sInvoiceNumber":"","dtInvoiceDate":"","mInvoiceAmount":"","dtDateReceived":"5/2/2016 7:18:41 AM","sStudioCategory2":"","sStudioCategory1":"","sOrderSource":"Redacted","sCustomerComments":"Redacted","sCustomerOrderName":"Redacted","sWorkOrderNumber":"Redacted","sCustomerNumber":"Redacted"}]

订单型号代码

public class ShipmentTrackingNumbers
{
    public string sTrackingNumber { get; set; }
}

public class Order
{
    public List<ShipmentTrackingNumbers> ShipmentTrackingNUmbers { get; set; }

    public string dtDateShipped
    {
        get;
        set;
    }
    public string dSPRatedWeight
    {
        get;
        set;
    }
    public string nMBTotalBoxes
    {
        get;
        set;
    }
    public string sShipToAddressLine2
    {
        get;
        set;
    }
    public string sShipToCountry
    {
        get;
        set;
    }
    public string ShipToZip
    {
        get;
        set;
    }
    public string sShipToState
    {
        get;
        set;
    }
    public string sShipToCity
    {
        get;
        set;
    }
    public string sShipToAddressLine1
    {
        get;
        set;
    }
    public string sShipToName
    {
        get;
        set;
    }
    public string sServiceLevel
    {
        get;
        set;
    }
    public string sInvoiceNumber
    {
        get;
        set;
    }
    public string dtInvoiceDate
    {
        get;
        set;
    }
    public string mInvoiceAmount
    {
        get;
        set;
    }
    public string dtDateReceived
    {
        get;
        set;
    }
    public string sStudioCategory2
    {
        get;
        set;
    }
    public string sStudioCategory1
    {
        get;
        set;
    }
    public string sOrderSource
    {
        get;
        set;
    }
    public string sCustomerComments
    {
        get;
        set;
    }
    public string sCustomerOrderName
    {
        get;
        set;
    }
    public string sWorkOrderNumber
    {
        get;
        set;
    }
    public string sCustomerNumber
    {
        get;
        set;
    }
}

提前感谢您的帮助。 任何指导都会很棒。

2 个答案:

答案 0 :(得分:2)

如果您在下面格式化的JSON中看到它的结果数组,那么您需要遍历它以访问任何属性。

   @foreach (var order in Model)
     {
       @Model.sWorkOrderNumber // (Wrong)
       @order.sWorkOrderNumber // (Right)
     }

并循环用户当前对象访问属性。

&#13;
&#13;
[
  {
    "ShipmentTrackingNUmbers": [
      {
        "sTrackingNumber": "Redacted"
      }
    ],
    "dtDateShipped": "5/12/2017 12:00:00 AM",
    "dSPRatedWeight": "41.0000",
    "nMBTotalBoxes": "1",
    "sShipToAddressLine2": "Redacted",
    "sShipToCountry": "U.S.A.",
    "sShipToZip": "Redacted",
    "sShipToState": "Redacted",
    "sShipToCity": "Redacted",
    "sShipToAddressLine1": "Redacted",
    "sShipToName": "Redacted",
    "sServiceLevel": "GND",
    "sInvoiceNumber": "",
    "dtInvoiceDate": "",
    "mInvoiceAmount": "",
    "dtDateReceived": "5/8/2017 3:16:25 PM",
    "sStudioCategory2": "",
    "sStudioCategory1": "",
    "sOrderSource": "Redacted",
    "sCustomerComments": "Redacted",
    "sCustomerOrderName": "Redacted",
    "sWorkOrderNumber": "Redacted",
    "sCustomerNumber": "Redacted"
  },
  {
    "ShipmentTrackingNUmbers": [
      {
        "sTrackingNumber": "Redacted"
      }
    ],
    "dtDateShipped": "5/10/2016 12:00:00 AM",
    "dSPRatedWeight": "3.0000",
    "nMBTotalBoxes": "1",
    "sShipToAddressLine2": "",
    "sShipToCountry": "U.S.A.",
    "sShipToZip": "Redacted",
    "sShipToState": "Redacted",
    "sShipToCity": "Redacted",
    "sShipToAddressLine1": "Redacted",
    "sShipToName": "Redacted",
    "sServiceLevel": "GND",
    "sInvoiceNumber": "",
    "dtInvoiceDate": "",
    "mInvoiceAmount": "",
    "dtDateReceived": "5/2/2016 7:18:41 AM",
    "sStudioCategory2": "",
    "sStudioCategory1": "",
    "sOrderSource": "Redacted",
    "sCustomerComments": "Redacted",
    "sCustomerOrderName": "Redacted",
    "sWorkOrderNumber": "Redacted",
    "sCustomerNumber": "Redacted"
  }
]
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的模型属于IEnumerable<Project.Models.Order>类型,它是集合,您已使用foreach对您的集合进行迭代。

因此,您需要使用循环变量来使用@Model变量来访问集合中的每个订单值,而不是引用order

@foreach (var order in Model)
{
    @order.sWorkOrderNumber
}

你有多个订单对象来自IEnumerable<T>,你需要通过foreach访问每一个,就像你正在做的那样但是在循环体内你使用了错误的变量。

希望它有所帮助。