繁体   English   中英

如何在 mvc 中列出这个 json 数据?

[英]How do I list this json data in mvc?

我正在尝试将此 json 数据列为 mvc。 如何编码视图模型? 我已经编写了 json 数据和视图模型编码。

查看型号:

public class OrderViewModel
{
    public List<Content> content { get; set; }
}


public class Content
{
    public string shipmentAddress { get; set; }
    public string firstName { get; set; }
    public string  lastName { get; set; }
    public string address1 { get; set; }
}

JSON 数据:

{
    "page": 0,
    "size": 50,
    "totalPages": 1,
    "totalElements": 1,
    "content": [
        {
            "shipmentAddress": {
                "id": 80844024,
                "firstName": "Trendyol",
                "lastName": "Müşterisi",
                "address1": "DSM Grup Danışmanlık İletişim ve Satış Tic. A.Ş. Büyükdere Caddesi Noramin İş Merkezi No:237 Kat:B1 ",
                "address2": "",
                "city": " İstanbul ",
                "cityCode": 34,
                "district": "Şişli",
                "districtId": 54,
                "postalCode": "10D",
                "countryCode": "TR",
                "fullName": "Trendyol Müşterisi",
                "fullAddress": "DSM Grup Danışmanlık İletişim ve Satış Tic. A.Ş. Büyükdere Caddesi Noramin İş Merkezi No:237 Kat:B1   Şişli  İstanbul "
            },
            "orderNumber": "80869231",
            "totalPrice": 34.99,
            "taxNumber": null,
            "invoiceAddress": {
                "id": 80844023,
                "firstName": "Trendyol",
                "lastName": "Müşterisi",
                "company": "",
                "address1": "DSM Grup Danışmanlık İletişim ve Satış Tic. A.Ş. Büyükdere Caddesi Noramin İş Merkezi No:237 Kat:B1 ",
                "address2": "",
                "city": " İstanbul ",
                "district": "Şişli",
                "postalCode": "",
                "countryCode": "TR",
                "fullName": "Trendyol Müşterisi",
                "fullAddress": "DSM Grup Danışmanlık İletişim ve Satış Tic. A.Ş. Büyükdere Caddesi Noramin İş Merkezi No:237 Kat:B1   Şişli  İstanbul "
            },
            "customerFirstName": "Trendyol",
            "customerEmail": "pf+dym24k@trendyol.com.test-google-a.com",
            "customerId": 99993706,
            "customerLastName": "Müşterisi",
            "id": 11650604,
            "cargoTrackingNumber": 7280447182689,
            "cargoTrackingLink": "http://service.mngkargo.com.tr/iactive/popup/kargotakip.asp?k=",
            "cargoSenderNumber": "700861966410",
            "lines": [
                {
                    "quantity": 1,
                    "productId": 67984887,
                    "salesCampaignId": 201642,
                    "productSize": " one size",
                    "merchantSku": "merchantSku",
                    "productName": "Kadın Çivit Mavi Geometrik Desenli Kapaklı Clutch sku1234 sku1234, one size",
                    "productCode": 11954798,
                    "merchantId": 201,
                    "price": 34.99,
                    "currencyCode": "TRY",
                    "productColor": "No Color",
                    "id": 56040534,
                    "sku": "sku1234",
                    "vatBaseAmount": 8,
                    "barcode": "barcode1234",
                    "orderLineItemStatusName": "ReturnAccepted"
                }
            ],
            "orderDate": 1542801149863,
            "tcIdentityNumber": "99999999999",
            "currencyCode": "TRY",
            "packageHistories": [
                {
                    "createdDate": 1542790350607,
                    "status": "Created"
                },
                {
                    "createdDate": 1543789070462,
                    "status": "Delivered"
                },
                {
                    "createdDate": 1542872460911,
                    "status": "Picking"
                },
                {
                    "createdDate": 1542953901874,
                    "status": "Shipped"
                }
            ],
            "shipmentPackageStatus": "ReturnAccepted"
        }
    ]
}

这个错误

读取字符串时出错。 意外标记:StartObject。 路径 ' content[0].shipmentAddress ',第 1 行,位置 90

像这样创建模型。

  public class ShipmentAddress
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string address1 { get; set; }
    public string address2 { get; set; }
    public string city { get; set; }
    public int cityCode { get; set; }
    public string district { get; set; }
    public int districtId { get; set; }
    public string postalCode { get; set; }
    public string countryCode { get; set; }
    public string fullName { get; set; }
    public string fullAddress { get; set; }
}

public class InvoiceAddress
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string company { get; set; }
    public string address1 { get; set; }
    public string address2 { get; set; }
    public string city { get; set; }
    public string district { get; set; }
    public string postalCode { get; set; }
    public string countryCode { get; set; }
    public string fullName { get; set; }
    public string fullAddress { get; set; }
}

public class Line
{
    public int quantity { get; set; }
    public int productId { get; set; }
    public int salesCampaignId { get; set; }
    public string productSize { get; set; }
    public string merchantSku { get; set; }
    public string productName { get; set; }
    public int productCode { get; set; }
    public int merchantId { get; set; }
    public double price { get; set; }
    public string currencyCode { get; set; }
    public string productColor { get; set; }
    public int id { get; set; }
    public string sku { get; set; }
    public int vatBaseAmount { get; set; }
    public string barcode { get; set; }
    public string orderLineItemStatusName { get; set; }
}

public class PackageHistory
{
    public object createdDate { get; set; }
    public string status { get; set; }
}

public class Content
{
    public ShipmentAddress shipmentAddress { get; set; }
    public string orderNumber { get; set; }
    public double totalPrice { get; set; }
    public object taxNumber { get; set; }
    public InvoiceAddress invoiceAddress { get; set; }
    public string customerFirstName { get; set; }
    public string customerEmail { get; set; }
    public int customerId { get; set; }
    public string customerLastName { get; set; }
    public int id { get; set; }
    public long cargoTrackingNumber { get; set; }
    public string cargoTrackingLink { get; set; }
    public string cargoSenderNumber { get; set; }
    public List<Line> lines { get; set; }
    public long orderDate { get; set; }
    public string tcIdentityNumber { get; set; }
    public string currencyCode { get; set; }
    public List<PackageHistory> packageHistories { get; set; }
    public string shipmentPackageStatus { get; set; }
}

public class RootObject
{
    public int page { get; set; }
    public int size { get; set; }
    public int totalPages { get; set; }
    public int totalElements { get; set; }
    public List<Content> content { get; set; }
}

根据我的理解,您正在尝试基于您的 JSON 生成一个 C# 类。

在这里,我发布了一个完整的 C# 类,您可以使用它来绑定特定的 JSON。

public class ShipmentAddress
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string address1 { get; set; }
    public string address2 { get; set; }
    public string city { get; set; }
    public int cityCode { get; set; }
    public string district { get; set; }
    public int districtId { get; set; }
    public string postalCode { get; set; }
    public string countryCode { get; set; }
    public string fullName { get; set; }
    public string fullAddress { get; set; }
}

public class InvoiceAddress
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string company { get; set; }
    public string address1 { get; set; }
    public string address2 { get; set; }
    public string city { get; set; }
    public string district { get; set; }
    public string postalCode { get; set; }
    public string countryCode { get; set; }
    public string fullName { get; set; }
    public string fullAddress { get; set; }
}

public class Line
{
    public int quantity { get; set; }
    public int productId { get; set; }
    public int salesCampaignId { get; set; }
    public string productSize { get; set; }
    public string merchantSku { get; set; }
    public string productName { get; set; }
    public int productCode { get; set; }
    public int merchantId { get; set; }
    public double price { get; set; }
    public string currencyCode { get; set; }
    public string productColor { get; set; }
    public int id { get; set; }
    public string sku { get; set; }
    public int vatBaseAmount { get; set; }
    public string barcode { get; set; }
    public string orderLineItemStatusName { get; set; }
}

public class PackageHistory
{
    public object createdDate { get; set; }
    public string status { get; set; }
}

public class Content
{
    public ShipmentAddress shipmentAddress { get; set; }
    public string orderNumber { get; set; }
    public double totalPrice { get; set; }
    public object taxNumber { get; set; }
    public InvoiceAddress invoiceAddress { get; set; }
    public string customerFirstName { get; set; }
    public string customerEmail { get; set; }
    public int customerId { get; set; }
    public string customerLastName { get; set; }
    public int id { get; set; }
    public long cargoTrackingNumber { get; set; }
    public string cargoTrackingLink { get; set; }
    public string cargoSenderNumber { get; set; }
    public List<Line> lines { get; set; }
    public long orderDate { get; set; }
    public string tcIdentityNumber { get; set; }
    public string currencyCode { get; set; }
    public List<PackageHistory> packageHistories { get; set; }
    public string shipmentPackageStatus { get; set; }
}

public class RootObject
{
    public int page { get; set; }
    public int size { get; set; }
    public int totalPages { get; set; }
    public int totalElements { get; set; }
    public List<Content> content { get; set; }
}

请检查它,如果您仍然面临共享课程的问题,请告诉我。

这是将 json 转换为 c# 的非常有用的站点。

http://json2csharp.com/

将您的代码粘贴到那里并生成,您将获得所需的模型。

您可以在 Visual Studio 2015 中轻松,复制 json 数据并打开 Visual Studio 2015,单击 Edit ----> Paste Special ----> Paste JSON as Classes

您可以对 xml 文件执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM