简体   繁体   中英

How do I list this json data in mvc?

I'm trying to list this json data as mvc. How to code view model? I have written json data and viewmodel coded.

View Model :

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 Data :

{
    "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"
        }
    ]
}

This error

Error reading string. Unexpected token: StartObject. Path ' content[0].shipmentAddress ', line 1, position 90

create the model like this.

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

As per my understanding you are trying to generate a C# class based on your JSON.

Here I am posting an entire C# Class which you can use for binding of specific 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; }
}

Kindly check it and do let me know if you are still facing issue with the shared class.

This is very useful site to convert json to c#.

http://json2csharp.com/

Paste your code there and generate, you will get the models required.

You can it easy in Visual Studio 2015, copy json data and open Visual Studio 2015, click Edit ----> Paste Special ----> Paste JSON as Classes

You can do this for xml files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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