我有以下代码:
public class Post
{
public int countSubject;
public string dayName;
public string[] subjectName;
public string[] auditory;
public string[] subjectType;
public string[] TimeStart;
public string[] TimeEnd;
public string[] Teacher;
public Post(int countSubject, string dayName, string[] subjectName, string[] auditory, string[] subjectType, string[] TimeStart,
string[] TimeEnd, string[] Teacher)
{
this.dayName = dayName;
this.countSubject = countSubject;
this.subjectName = subjectName;
this.auditory = auditory;
this.subjectType = subjectType;
this.TimeStart = TimeStart;
this.TimeEnd = TimeEnd;
this.Teacher = Teacher;
}
}
List<Post> posts = new List<Post>();
posts.Add(new Post(5, "Monday", new string[] { "Свободная", "Физика", "ИНТЕХ", "ИНФОРМАТИКА" },
new string[] { "", "320", "3201", "108" }, new string[] { "", "Лекция", "Практика", "Лабораторная" },
new string[] { "08:05", "09:50", "11:35", "13:35" }, new string[] { "9:35", "11:20", "13:05", "15:05" },
new string[] { "", "Повх Л.А", "Ямполь Е.С", "Лихозвон" }));
posts.Add(new Post(5, "Tuesday", new string[] { "УПИФ", "ИНФОРМАТИКА", "МАТАН", "МАТАН" },
new string[] { "", "320", "3201", "108" }, new string[] { "Лекция", "Практика", "Лекция", "Практика" },
new string[] { "08:05", "09:50", "11:35", "13:35" }, new string[] { "9:35", "11:20", "13:05", "15:05" },
new string[] { "Лолка А.А", "Повх Л.А", "Ямполь Е.С", "Лихозвон" }));
JObject rss = new JObject(
new JProperty("Timetable",
new JArray(
new JObject(
new JProperty("CountSubject", "5"),
from q in posts
select
new JProperty(q.dayName,
from d in q.subjectType
select
new JObject(
new JProperty("SubjectName", ""),
new JProperty("Auditory", ""),
new JProperty("SubjectType", d),
new JProperty("TimeStart", ""),
new JProperty("TimeEnd", ""),
new JProperty("Teacher", "")))))));
在控制台中获取此输出:
{
"Timetable": [
{
"CountSubject": "5",
"Monday": [
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лабораторная",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
}
],
"Tuesday": [
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
}
]
}
]
}
问题是,主题的类型被正确填充,但是该字段的其余部分无法填充,因为在“q.subjectType中的d”之后添加第二个“from”和q.subjectName等会再次进行迭代循环并获取重复的JSON对象。
必须如此:
{
"Timetable": [
{
"CountSubject": "5",
"Monday": [
{
"SubjectName": "Свободная",
"Auditory": "",
"SubjectType": "",
"TimeStart": "08:05",
"TimeEnd": "09:35",
"Teacher": ""
},
{
"SubjectName": "Физика",
"Auditory": "320",
"SubjectType": "Лекция",
"TimeStart": "09:50",
"TimeEnd": "11:20",
"Teacher": "Повх Л.А"
},
{
"SubjectName": "ИНТЕХ",
"Auditory": "3201",
"SubjectType": "Практика",
"TimeStart": "11:35",
"TimeEnd": "13:05",
"Teacher": "Ямполь Е.С"
},
{
"SubjectName": "ИНФОРМАТИКА",
"Auditory": "108",
"SubjectType": "Лабораторная",
"TimeStart": "13:35",
"TimeEnd": "15:05",
"Teacher": "Лихозвон"
}
],
"Tuesday": [
{
"SubjectName": "УПИФ",
"Auditory": "320",
"SubjectType": "Лекция",
"TimeStart": "08:05",
"TimeEnd": "09:35",
"Teacher": "Лолка А.А"
},
{
"SubjectName": "ИНФОРМАТИКА",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "09:50",
"TimeEnd": "11:20",
"Teacher": "Повх Л.А"
},
{
"SubjectName": "МАТАН",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "11:35",
"TimeEnd": "13:05",
"Teacher": "Ямполь Е.С"
},
{
"SubjectName": "МАТАН",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "13:35",
"TimeEnd": "15:05",
"Teacher": "Лихозвон"
}
]
}
]
}
抱歉,我的英文不好,谷歌翻译
答案 0 :(得分:0)
当你使用两个部分&#34;来自&#34;你将获得笛卡尔乘法而不是加入两个集合。 要得到你想要的东西,试着像这样重写:
from index in Enumerable.Range(0, q.subjectType.Length)
select
new JObject(
new JProperty("SubjectName", q.subjectName[index]),
new JProperty("Auditory", q.auditory[index]),
new JProperty("SubjectType", q.subjectType[index]),
new JProperty("TimeStart", q.TimeStart[index]),
new JProperty("TimeEnd", q.TimeEnd[index]),
new JProperty("Teacher", q.Teacher[index])))))));