gpt4 book ai didi

asp.net - 从代码隐藏文件创建 JSON 数组

转载 作者:行者123 更新时间:2023-12-02 10:30:18 29 4
gpt4 key购买 nike

如何使用 asp.net、c# 创建如下所示的 arraylist。我可以使用 JsonTextWriter 创建这样的数组,但不想添加任何其他 dll 作为我的解决方案的一部分。

  [{ "id": "slide-img-1", "client": "nature beauty", "desc": "nature beauty photography" },
{ "id": "slide-img-2", "client": "nature beauty", "desc": "add your description here" },
{ "id": "slide-img-3", "client": "nature beauty", "desc": "add your description here" },
{ "id": "slide-img-4", "client": "nature beauty", "desc": "add your description here" },
{ "id": "slide-img-5", "client": "nature beauty", "desc": "add your description here" },
{ "id": "slide-img-6", "client": "nature beauty", "desc": "add your description here" },
{ "id": "slide-img-7", "client": "nature beauty", "desc": "add your description here"}];

谢谢

阿什什

最佳答案

您可以使用a JavaScriptSerializer :

假设数组中的实体类似于:

public class Entity 
{
public string id { get; set; }
public string client { get; set; }
public string desc {get; set; }

[ScriptIgnore]
public string PropertyThatIsIgnored { get;set; }
}

然后就可以像这样使用了:

Entity[] entities = ...;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(entities);

或者,如果需要更复杂的序列化,您可以考虑使用 a DataContractJsonSerializer ,因为它允许 more extensibility when serializing / deserializing .

关于asp.net - 从代码隐藏文件创建 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9017949/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com