C# 我可以用JSON序列化包含列表的对象吗?
我在使用JsonConvert.SerializeObject(addToBasketView)时遇到了一个问题,因为它似乎不喜欢我传递给它的列表-直到我传递了列表,它才正常工作-你不能传递列表吗 这是我的密码: 控制器:C# 我可以用JSON序列化包含列表的对象吗?,c#,asp.net-mvc,json,serialization,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 = Auto
//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; }
}
}
使用系统;
使用System.Collections.Generic;
命名空间WidgetData
{
///
///这是我们将向AddToBasketForm提供的视图
///
[可序列化]
公共类AddToBasketView
{
///
///零售商的用户名
///
公共字符串用户名{get;set;}
///
///零售商的密码
///
公共字符串密码{get;set;}
///
///他们希望添加到站点的内容的URl
///
公共字符串Url{get;set;}
///
///选择的零售商产品-从中我可以获得零售商和产品。
///
公共RetailerProductJson RetailerProductJson{get;set;}
///
///小部件impression id,以便我们可以附加到addTobaskets表以进行记录
///
public int WidgetImpressionId{get;set;}
///
///设定数量
///
公共整数数量{get;set;}
///
///MessageId是这样的,我们可以在等待响应时识别它
///
公共字符串MessageId{get;set;}
///
///自动化脚本列表
///
公共列表自动列表{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; }
}
}
使用系统;
使用System.ComponentModel.DataAnnotations;
使用System.Web.Mvc;
命名空间WidgetData
{
///
///将Linq创建的类扩展到SQL,以便我们可以在模型部分控制数据的有效性
///
[元数据类型(typeof(AuotmationStepScriptValidation))]
公共部分类AutomationStepScript
{
///
///重写to字符串,以便我们可以在删除之前在delete方法中捕获此记录的详细信息
///
///
公共重写字符串ToString()
{
返回string.Format(“AutomationStepScript Id={0},AutomationStepId={1},排序顺序={2},描述={3},脚本={4},求值={5}”,Id,AutomationStepId,排序器,描述,脚本,求值);
}
}
///
///类来验证我的AuotmationStepScript
///
公共类AuotmationStepScriptValidation
{
///
///自动识别
///
[必需(ErrorMessage=“AutomationStepId是必填字段”)]
[范围(1,int.MaxValue,ErrorMessage=“AutomationStepId必须有效”)]
public int AutomationStepId{get;set;}
///
///显示排序器
///
[范围(0,int.MaxValue,ErrorMessage=“排序器必须有效”)]
公共int排序器{get;set;}
///
///描述
///
[StringLength(256,ErrorMessage=“说明最大长度为256”,最小长度=1)]
公共字符串说明{get;set;}
///
///剧本
///
[allowtml]
[StringLength(8000,ErrorMessage=“脚本最大长度为8000”)]
公共字符串脚本{get;set;}
///
///评估
///
公共int求值{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;
}
//
///helper函数用于获取数据,以便我们可以获取此自动化的每个步骤的脚本(我们将从localParams中找到)
///
///
///
///我们返回AutomationStepScripts列表,将使用Lambda表达式“>
公共列表AutomationHelper(int-retailerId、int-productId)
{
List automationStepScripts=null;
var automationStepScriptRepository=new automationStepScriptRepository();
var productRepository=new productRepository();
var retailerCategoryRepository=新的retailerCategoryRepository();
var product=productRepository.GetProduct(productId);
var categoryId=product.categoryId;
var retailerCategory=retailerCategoryRepository.GetRetailerCategoryByRetailerCategory(retailerId,categoryId);
//我们有零售商类别吗?
if(retailerCategory!=null)
{
//是的,如果没有有效的零售商类别,我们就不可能找到自动化产品
//现在我们有了RetailerCategory。下一个问题是这个RetailerCategory是否有一个自动化设置
if(retailerCategory.AutomationId!=null)
{
//是的,我们有一个自动化系统。所以让我们来获取这个自动化的所有步骤的所有脚本
//获取此自动化的所有步骤的所有脚本。
automationStepScripts=automationStepScriptRepository.GetAllAutomationStepScriptsFrautomation((int)retailerCategory.AutomationId).ToList();
}
}
返回自动步骤脚本;
}
我将它序列化,因为它是我在SQS中使用的消息体,稍后我将使用它
如果你需要更多的信息,请告诉我
谢谢。你能举个例子说明JSON在列表中是什么样子吗?另外,出于好奇,你是否将
AutomationStepScript
标记为Serializable
?你当然可以序列化列表,是列表没有序列化吗?如果是这样,如果你也发布了类定义,可能会有所帮助。@Stony请查看我对列表的编辑或者更多的代码我还没有试着序列化clas