繁体   English   中英

在C#中将列表转换为json格式

[英]convert list to json format in c#

在C#中,我填写一个列表

List<CareerOpportunities> jobs = new List<CareerOpportunities>();

与持有工作机会的对象。 我想将“工作”转换为JSON格式,如下所示:

    {
    "Job Title":{
    "Sex":"...",
    "City":"...",
    "Date":"...",
    "ActivityField":"...",
    "Salary":"...",
    "WorkHours":"...",
    "Insurance":"..."
    "Address":"..."
    },
    .
    .
    .
    .
    }

我怎样才能做到这一点?

我尝试过,但是得到了以下结果:

[
{
"JobTitle":"...",
"Sex":"...",
"City":"...",
"Date":"...",
"ActivityField":"...",
"Salary":"... ",
"WorkHours":"...",
"Insurance":"...",
"Address":"...
},
.
.
.
.
]

我认为您应该坚持得到的JSON序列化结果。 例如,这就是服务的消费者期望获得的。 您已经定义了一个项目列表,并且应该将其序列化为JSON中的对象数组。 如果要获得类似于您编写的代码的内容,则应定义一个对象结构,例如:

class CareerOportunities {
    public List<CareerOportunity> oportunities = new List<CareerOportunity>();
}

class CareerOportunity {
    public string JobTitle { get; set; }
    // more attributes here ...
}

这将被序列化为:

{
    "oportunities": [
        {
            "JobTitle": "..."
        },
        {
            "JobTitle": "..."
        }
        ...
    ]
}

拥有“ []”是正常的,因为列表已转换为数组,没有“ []”就无法拥有列表

如果只想使用“ {}”,则只需返回一个对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM