【问题标题】:JSON show only the value without the keyJSON 只显示没有键的值
【发布时间】:2013-06-30 16:56:08
【问题描述】:

再见。

我的 WebApi 控制器中有一个带有实体框架的 JSON 输出。 如下所示:

"Answers":[{"Text":"text text text"},{"Text":"text text text"},{"Text":"text text text"}]}]

我想这样展示它:

"Answers":["text text text","text text text","text text text"]

这是我的实体模型(答案类):

public class Answers
{
    [Key]
    [JsonIgnore]
    public int Id { get; set; }

    public string Text { get; set; }

    // An Answers related to a Question
    public virtual Question Question { get; set; }
}

【问题讨论】:

    标签: c# json entity-framework asp.net-web-api


    【解决方案1】:

    您正在尝试将对象数组转换为字符串数组。

    您可以通过创建匿名类型来做到这一点:

    new { 
        question.Text,
        Answers = question.Answers.Select(a => a.Text)
    }
    

    【讨论】:

    • @DanielChernenkov:这会创建一个可以序列化为 JSON 的新对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多