0

Could someone helpe me?

How can I code 'Shippings' class as an Array to get the json example below?

{
  "seller_id": "123",
  "amount": 100,
  "order": {
    "order_id": "1111",
    "sales_tax": 0,
    "product_type": "service"
  },
  "shippings": [{
    "first_name": "John",
    "phone_number": "5551999887766",
    "shipping_amount": 3000,
    "address": {
      "street": "Street 35 Conts",
      "number": "1000",
      "complement": "ap1",
      "postal_code": "90230060"
    }
  }],
  "credit": {
    "delayed": false,
    "authenticated": false
  }
}

I am doing this below, using asp.net mvc, but don't know how to get Shippings as Array [].

Can someone give me some exemplo or anything else... I'll appreciate.

var request = new GetNetRoot() {
  SellerId = seller_id,
    Amount = orderItens.Amount
  Order = new GetNetPagOrder() {
      OrderId = order.id.ToString(),
        SalesTax = orderItens.Tax,
        ProductType = orderItens.ProdType
    },
    Shippings = new GetNetPagShippings() {
      FirstName = "",
        PhoneNumber = usr.PhoneNumber,
        ShippingAmount = orderItens.AmountShip,
        Address = new GetNetPagAddress() {
          Street = catEnd.IdEnderecoLogradouroNavigation.IdRuaNavigation.Nome,
            Number = catEnd.NumEndereco,
            Complement = catEnd.Complemento,
            PostalCode = catEnd.IdEnderecoLogradouroNavigation.Cep
        }
    },
    Credit = new GetNetPagCredit() {
      Delayed = false,
        Authenticated = false
    }
};

var requestBody = JsonConvert.SerializeObject(request)
1
  • 1
    Edit-Paste Special-Paste JSON as classes menu item in visual studio to get a proper class model. GetNetPagShippings should return an array, not the single item Commented Apr 25, 2020 at 17:30

3 Answers 3

2

You should initialize Shippings like an array:

Shippings = new[] {
    new GetNetPagShippings()
    {
        FirstName = "",                              
        PhoneNumber = usr.PhoneNumber,
        ShippingAmount = orderItens.AmountShip,
        Address = new GetNetPagAddress()
        {
            Street = catEnd.IdEnderecoLogradouroNavigation.IdRuaNavigation.Nome,       
            Number = catEnd.NumEndereco,                                                
            Complement = catEnd.Complemento,                                           
            PostalCode = catEnd.IdEnderecoLogradouroNavigation.Cep                      
        }
    }



public class GetNetPagamentoRoot
{
    ...

    public GetNetPagShippings[] Shippings { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array initializer syntax. Simplistic example would be array of int:

int[] myArray = new [] { 1, 2, 3 }

In your code Shippings should be initialized as an array.

var request = new GetNetRoot()
{
    SellerId = seller_id,
    Amount = orderItens.Amount
    Order = new GetNetPagOrder()
    {
        OrderId = order.id.ToString(),
        SalesTax =  orderItens.Tax,
        ProductType =  orderItens.ProdType
    },
    // Array initializer with 2 elements.
    Shippings = new[] {
        new GetNetPagShippings()
        {
            FirstName = "",                              
            PhoneNumber = usr.PhoneNumber,
            ShippingAmount = orderItens.AmountShip,
            Address = new GetNetPagAddress()
            {
                Street = catEnd.IdEnderecoLogradouroNavigation.IdRuaNavigation.Nome,       
                Number = catEnd.NumEndereco,                                                
                Complement = catEnd.Complemento,                                           
                PostalCode = catEnd.IdEnderecoLogradouroNavigation.Cep                      
            }
        },
        new GetNetPagShippings()
        {
            FirstName = "",                              
            PhoneNumber = usr.PhoneNumber,
            ShippingAmount = orderItens.AmountShip,
            Address = new GetNetPagAddress()
            {
                Street = catEnd.IdEnderecoLogradouroNavigation.IdRuaNavigation.Nome,       
                Number = catEnd.NumEndereco,                                                
                Complement = catEnd.Complemento,                                           
                PostalCode = catEnd.IdEnderecoLogradouroNavigation.Cep                      
            }
        }
    },
    Credit = new GetNetPagCredit()
    {
        Delayed = false,
        Authenticated = false
    }
};

var requestBody = JsonConvert.SerializeObject(request);

Comments

0

Your GetNetRoot outer class needs to define the "Shippings" property as a list or array of the GetNetPagShippings class as opposed to a single instance. When you serialize it to JSON, it will be represented as a JSON array of the object.

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var myClassInstance = new MyOuterClass()
            {
                OuterProperty = "Outer Property",
                MyInnerClassList = new List<MyInnerClass>()
                {
                    new MyInnerClass()
                    {
                        InnerProperty = "Inner Property Item 1"
                    },
                    new MyInnerClass()
                    {
                        InnerProperty = "Inner Property Item 2"
                    }
                }
            };

            string json = JsonConvert.SerializeObject(myClassInstance);
            Console.WriteLine(json);
        }
    }

    public class MyOuterClass
    {
        public string OuterProperty { get; set; }
        public List<MyInnerClass> MyInnerClassList { get; set; }
    }

    public class MyInnerClass
    {
        public string InnerProperty { get; set; }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.