将具有两个字符串参数的对象转换为逗号分隔的字符串

这是我的对象,我正在获取此对象的列表:


public class MeetingTypeModel

{

    public string MeetingTypeId { get; set; }

    public string MeetingTypeName { get; set; }

}


List<MeetingTypeModel> MeetingTypeList

我想把它转换成格式:


({ "1": "Bangladesh", "2": "Belgium"})


拉风的咖菲猫
浏览 173回答 3
3回答

哆啦的时光机

如果您想避免使用外部库,您可以逐项进行,如下所示:&nbsp; &nbsp; StringBuilder SB = new StringBuilder();&nbsp; &nbsp; SB.Append("({");&nbsp; &nbsp; for(int i=0; i<MeetingTypeList.Count; i++) {&nbsp; &nbsp; &nbsp; &nbsp; MeetingTypeModel mtm = MeetingTypeList[i];&nbsp; &nbsp; &nbsp; &nbsp; SB.AppendFormat("\"{0}\":"\"{1}\", mtm.MeetingTypeId, mtm.MeetingTypeName);&nbsp; &nbsp; &nbsp; &nbsp; if {i<MeetingTypeList.Count -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SB.Append(",");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; SB.Append("})");
打开App,查看更多内容
随时随地看视频慕课网APP