You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中创建指定嵌套结构的JSON数组

实现步骤

1. 定义对应的数据模型类

你需要先定义两个实体类,分别对应answers数组里的单个答案对象,以及外层的问题对象:

// 答案对象对应的类
public class Answer
{
    public string Id { get; set; }
    public string Text { get; set; }
    public bool Correct { get; set; }
    public bool UserInput { get; set; }
}

// 问题对象对应的类
public class Question
{
    public string QuestionText { get; set; }
    public bool IsAnswered { get; set; }
    // 嵌入答案对象数组,也可以声明为List<Answer>类型,序列化后效果一致
    public Answer[] Answers { get; set; }
}

如果需要序列化后的JSON属性名和示例完全匹配(小写开头、命名完全对应),可以给属性加对应序列化特性:

// 适配.NET自带System.Text.Json的写法示例
public class Answer
{
    [JsonPropertyName("id")]
    public string Id { get; set; }
    [JsonPropertyName("text")]
    public string Text { get; set; }
    [JsonPropertyName("correct")]
    public bool Correct { get; set; }
    [JsonPropertyName("userInput")]
    public bool UserInput { get; set; }
}

public class Question
{
    [JsonPropertyName("question")]
    public string QuestionText { get; set; }
    [JsonPropertyName("isAnswered")]
    public bool IsAnswered { get; set; }
    [JsonPropertyName("answers")]
    public Answer[] Answers { get; set; }
}

如果使用Newtonsoft.Json,把JsonPropertyName替换为JsonProperty即可。

2. 构造数据并序列化为JSON

以.NET自带的System.Text.Json为例:

// 构造和示例一致的测试数据
var questions = new List<Question>
{
    new Question
    {
        QuestionText = "What kind of fruit was used to name in 1984?",
        IsAnswered = false,
        Answers = new Answer[]
        {
            new Answer { Id = "1", Text = "Blackber123ry", Correct = false, UserInput = false },
            new Answer { Id = "2", Text = "Blueberry", Correct = false, UserInput = false },
            new Answer { Id = "3", Text = "Pear", Correct = false, UserInput = false },
            new Answer { Id = "4", Text = "Apple", Correct = true, UserInput = false }
        }
    },
    new Question
    {
        QuestionText = "SECOND QUUESTION",
        IsAnswered = false,
        Answers = new Answer[]
        {
            new Answer { Id = "0", Text = "192.168.1.1", Correct = false, UserInput = false },
            new Answer { Id = "1", Text = "127.0.0.1", Correct = true, UserInput = false },
            new Answer { Id = "2", Text = "209.85.231.104", Correct = false, UserInput = false },
            new Answer { Id = "3", Text = "66.220.149.25", Correct = false, UserInput = false }
        }
    }
};

// 序列化配置,开启缩进格式化、自动驼峰命名转换
var jsonOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
// 生成目标JSON字符串
string jsonResult = JsonSerializer.Serialize(questions, jsonOptions);

如果使用Newtonsoft.Json,序列化代码调整为:

string jsonResult = JsonConvert.SerializeObject(questions, Formatting.Indented);

内容的提问来源于stack exchange,提问作者Nikola Marinov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 19:27:01