我可以JSON序列化包含列表的对象吗?

时间:2013-10-21 11:20:02

标签: c# asp.net-mvc json serialization

我在使用JsonConvert.SerializeObject(addToBasketView)时遇到了一个问题,因为它似乎不喜欢我传入它的列表 - 直到我通过列表它工作正常 - 你是不是能够通过一个列出?

这是我的代码:

控制器:

//We need a list of Automation scripts - this was in Apollo but now we need to pass it in teh message
           var automationList = AutomationHelper(retailerId, productId);

 var addToBasketView = new AddToBasketView
            {
                Url = retailerProduct.DeepLink,
                Password = (password),
                Username = (username),
                RetailerProductJson = new RetailerProductJson(retailerProduct),
                WidgetImpressionId = id,
                Quantity = qty,
                MessageId = messageId,
                AutomationList = automationList
            };


            // now turn it into a string
            var json = JsonConvert.SerializeObject(addToBasketView);

Addtobasketview.cs:

using System;
using System.Collections.Generic;

namespace WidgetData
{
    /// <summary>
    /// This is the view that we will give to the AddToBasketForm
    /// </summary>
    [Serializable]
    public class AddToBasketView
    {


        /// <summary>
        /// The Username for the retailer
        /// </summary>
        public String Username { get; set; }

        /// <summary>
        /// The password for the retailer
        /// </summary>
        public String Password { get; set; }

        /// <summary>
        /// The URl of the thing they want added to the site
        /// </summary>
        public String Url { get; set; }

        /// <summary>
        /// The retailer product selected - from this I can get the retailer and the product.
        /// </summary>
        public RetailerProductJson RetailerProductJson { get; set; }

        /// <summary>
        /// The widget impression id so that we can attach to the addTobaskets table for recording purposes
        /// </summary>
        public int WidgetImpressionId { get; set; }

        /// <summary>
        /// set the quantity
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// The MessageId this is so we can identify it when waiting for a response
        /// </summary>
        public String MessageId { get; set; }

        /// <summary>
        /// Automation script list
        /// </summary>
        public List<AutomationStepScript> AutomationList { get; set; }

    }
}

修改 AutomationStepScript.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace WidgetData
{
    /// <summary>
    /// Extends the class created by Linq To SQL, so that we can control in our models section the validity of the data
    /// </summary>
    [MetadataType(typeof(AuotmationStepScriptValidation))]
    public partial class AutomationStepScript
    {
        /// <summary>
        /// Override the to string so that we can use in the delete method to capture details of this record before it is deleted
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
           return string.Format("AutomationStepScript Id = {0}, AutomationStepId = {1}, Sort Order = {2}, Description = {3}, Script = {4}, Evaluate = {5}", Id,AutomationStepId,SortOrder,Description,Script,Evaluate);
        }
    }

    /// <summary>
    /// Class to validate my AuotmationStepScript
    /// </summary>
    public class AuotmationStepScriptValidation
    {
        /// <summary>
        /// AutomationId
        /// </summary>
        [Required(ErrorMessage = "AutomationStepId is a required field")]
        [Range(1, int.MaxValue, ErrorMessage = "AutomationStepId must be valid")]
        public int AutomationStepId { get; set; }

        /// <summary>
        /// Display SortOrder
        /// </summary>
        [Range(0, int.MaxValue, ErrorMessage = "SortOrder must be valid")]
        public int SortOrder { get; set; }

        /// <summary>
        /// Description 
        /// </summary>
        [StringLength(256, ErrorMessage = "Description maximum length is 256", MinimumLength = 1)]
        public String Description { get; set; }

        /// <summary>
        /// Script
        /// </summary>
        [AllowHtml]
        [StringLength(8000, ErrorMessage = "Script maximum length is 8000")]
        public String Script { get; set; }

        /// <summary>
        /// Evaluate
        /// </summary>
        public int Evaluate { get; set; }
    }


}

AutomationHelper(在控制器中调用以获取列表:

 /// <summary>
        /// The helper function to fetch the data so that we can get the scripts for each step for this automation (which we will find from the localParams)
        /// </summary>
        /// <param name="retailerId"> </param>
        /// <param name="productId"> </param>
        /// <returns>WE return a List of AutomationStepScripts, this object will be selected further with Lambda expression"></returns>
        public List<AutomationStepScript> AutomationHelper(int retailerId, int productId)
        {
            List<AutomationStepScript> automationStepScripts = null;
            var automationStepScriptRepository = new AutomationStepScriptRepository();
            var productRepository = new ProductRepository();
            var retailerCategoryRepository = new RetailerCategoryRepository();
            var product = productRepository.GetProduct(productId);
            var categoryId = product.CategoryId;
            var retailerCategory = retailerCategoryRepository.GetRetailerCategoryByRetailerCategory(retailerId, categoryId);

            // DO we have a retailer category?
            if (retailerCategory != null)
            {
                // Yes, without a valid retailer category we cannot possibly find the automation
                // Now here we have the RetailerCategory. The next question is Does this RetailerCategory HAVE an automation set up for it
                if (retailerCategory.AutomationId != null)
                {
                    // Yes, we have an automation. So lets get all of the Scripts for all of the steps for this automation
                    // Get All scripts for all steps for this automation.
                    automationStepScripts = automationStepScriptRepository.GetAllAutomationStepScriptsForAutomation((int)retailerCategory.AutomationId).ToList();
                }
            }
            return automationStepScripts;
        }

我正在串行化它,因为它正在我在SQS中使用的消息的主体,然后我将使用它。

如果您需要更多信息,请与我们联系。

感谢。

0 个答案:

没有答案