将json转换为c#并在gridview中显示

时间:2018-06-10 07:41:08

标签: c# sql json web-services

我正在尝试将所有数据从webservice json显示到gridview或某些列显示到gridview:

namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
    public class LoginInfo
    {
        public string access_token { get; set; }
    }
    public enum ServiceResultEnums
    {
        Success,
        BadData,
        ServerError
    }
    public class VouchersResultSet
    {
        public VouchersResultSet()
        {
            Success = false;
            ResultCode = ServiceResultEnums.ServerError;
        }

        /// <summary>
        /// Does the web call was successful or not
        public bool Success { get; set; }

        /// <summary>
        /// Web call result
        public ServiceResultEnums ResultCode { get; set; }

        /// <summary>
        /// Helper message in english -- not required
        public string EnglishMessage { get; set; }

        /// <summary>
        /// Helper message in arabic - not required
        public string ArabicMessage { get; set; }

        /// <summary>
        /// Field to contain debug data 
        public string Debug { get; set; }

        /// <summary>
        /// Field to send any need info while debugging
        public object dump { get; set; }
        public List<VoucherViewModel> Vouchers { get; set; } =       new    List<VoucherViewModel>();

    }
    public class VoucherViewModel
    {

        public long Id { get; set; }
        public int GroupNumber { get; set; }
        public string GroupName { get; set; }
        public int CountryId { get; set; }
        public string CountryName { get; set; }
        public string Status { get; set; }

    }


    public class GetVouchersParam
    {
        public bool HideExpired { get; set; } = true;
        public bool HideCancelled { get; set; } = true;
        public VoucherPaymentStatus PaymentStatus { get; set; }
        public long VoucherNumber { get; set; }
        public DateTime? IssueDateFrom { get; set; }
        public DateTime? IssueDateTo { get; set; }
        public DateTime? PaymentDateFrom { get; set; }
        public DateTime? PaymentDateTo { get; set; }
        public bool ArabicLanguage { get; set; } = true;
        public short[] CountrysIds { get; set; } = new short[0];
        public int[] AgentsIds { get; set; } = new int[0];
    }
    /// <summary>
    /// Voucher Payment Status
    /// </summary>
    public enum VoucherPaymentStatus
    {
        All,
        Paid,
        Unpaid
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var userName = ""; //Set UserName here
            var password = ""; //Set Password here
            var client = new WebClient
            {
                Encoding = Encoding.UTF8
            };
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            //Login
            var loginResult = client.UploadString("https://Printo.com/test/token", "grant_type=password&username=" + userName + "&password=" + password);
            var loginInfo = JsonConvert.DeserializeObject<LoginInfo>(loginResult);

            //Get Vouchers
            if (loginInfo != null && loginInfo.access_token != null && loginInfo.access_token.Length > 0)
            {
                client.Headers.Clear();
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.Headers.Add("Authorization", "bearer " + loginInfo.access_token);
                var searchParams = new GetVouchersParam { IssueDateFrom = new DateTime(2018, 6, 1), IssueDateTo = new DateTime(2018, 6, 7), PaymentStatus = VoucherPaymentStatus.Paid };

                var result = client.UploadString("http://printo.com/printo/api/alltest/test1", JsonConvert.SerializeObject(searchParams));
                var vouchersResult = JsonConvert.DeserializeObject<VouchersResultSet>(result);
                GridView1.DataSource = vouchersResult;
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }

但是,执行以下代码时出错:

  

数据源是无效类型。它必须是IListSource,IEnumerable或IDataSource。

1 个答案:

答案 0 :(得分:0)

错误消息告诉您错误:您无法将网格视图的数据源设置为VouchersResultSet,因为这不会实现网格视图所知道的任何接口。 / p>

我怀疑你想要做的是将它设置为VouchersResultSet中的优惠券:

GridView1.DataSource = vouchersResult.Vouchers;

但是,您可能希望先检查结果是成功还是失败,以便在失败时显示相应的错误消息而不是空网格。