【发布时间】:2015-08-18 01:05:37
【问题描述】:
为了使用我正在开发的食谱网络服务,我有以下类来保存和序列化食谱数据:
class Recipe {
public string RecipeId { get; private set; }
public string UserId { get; set; }
public string Title { get; set; }
public IList<string> IngredientsList { get; set; }
public List<Group<string, Ingredient>> IngredientsWithHeaders { get; set; }
public List<Group<string, string>> InstructionsWithHeaders { get; set; }
public List<string> Notes { get; set; }
public ISet<string> Tags { get; set; }
public int Yield { get; set; }
public Recipe(string recipeId)
{
RecipeId = recipeId;
IngredientsWithHeaders = new List<Group<string,Ingredient>>();
InstructionsWithHeaders = new List<Group<string, string>>();
IngredientsList = new List<string>();
}
public byte[] Image { get; set; }
}
class Ingredient
{
public string Quantity { get; set; }
public string Modifier { get; set; }
public string Unit { get; set; }
public string IngredientName { get; set; }
public string Preparation { get; set; }
public Ingredient(string[] line)
{
if (!string.IsNullOrWhiteSpace(line.ElementAt(0)))
{
Quantity = line.ElementAt(0);
}
if (!string.IsNullOrWhiteSpace(line.ElementAt(1)))
{
Unit = line.ElementAt(1);
}
if (!string.IsNullOrWhiteSpace(line.ElementAt(2)))
{
IngredientName = line.ElementAt(2);
}
if(line.Length>3)
{
Preparation = line.Last();
}
}
}
class Group<K, T> : ObservableCollection<T>
{
public K Key { get; set; }
public Group(K key, IEnumerable<T> items) : base(items)
{
Key = key;
Debug.WriteLine(key);
}
}
我为 List<Group<string, Ingredient>> 得到的 JSON 输出是
{
"IngredientsWithHeaders": [
[
{
"Quantity": "3",
"Modifier": null,
"Unit": "tbsp",
"IngredientName": "butter",
"Preparation": null
},
{
"Quantity": "1",
"Modifier": null,
"Unit": "16 oz. bag",
"IngredientName": "marshmallows",
"Preparation": null
},
{
"Quantity": "2/3",
"Modifier": null,
"Unit": "cup",
"IngredientName": "dry cake mix",
"Preparation": null
},
{
"Quantity": "6",
"Modifier": null,
"Unit": "cups",
"IngredientName": "crispy rice cereal",
"Preparation": null
},
{
"Quantity": "1",
"Modifier": null,
"Unit": "container",
"IngredientName": "sprinkles",
"Preparation": "optional"
}
]
]
}
而我想要得到的更像是
{
"IngredientsWithHeaders": [
{
"Group": {
"Header": "BlankHeader",
"Items": [
{
"Quantity": "3",
"Modifier": null,
"Unit": "tbsp",
"IngredientName": "butter",
"Preparation": null
},
{
"Quantity": "1",
"Modifier": null,
"Unit": "16 oz. bag",
"IngredientName": "marshmallows",
"Preparation": null
},
{
"Quantity": "2/3",
"Modifier": null,
"Unit": "cup",
"IngredientName": "dry cake mix",
"Preparation": null
},
{
"Quantity": "6",
"Modifier": null,
"Unit": "cups",
"IngredientName": "crispy rice cereal",
"Preparation": null
},
{
"Quantity": "1",
"Modifier": null,
"Unit": "container",
"IngredientName": "sprinkles",
"Preparation": "optional"
}
]
}
}
]
}
我需要编写自定义序列化程序吗?如果是这样,我如何在不知道它是否是
的情况下将对象转换为参数化组Group<string, Ingredient>
或
Group<string, string>
?
【问题讨论】:
-
您要的 JSON 无效。尝试将其上传到jsonlint.com,您将收到错误消息。 JSON 容器可以是对象(一组无序的名称/值对)或数组——但不能同时是两者。根据jsonlint.com,您能否举一个您想要的有效 JSON 示例?
-
我的错误,即将使用更具体的 JSON 进行编辑。
标签: c# json serialization json.net