[C#]LINQ JSON 添加 JProperty

标签: json Linq C#
发布时间: 2017/2/27 5:39:27
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有以下代码︰

    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": ""
        }
      ]
    }
  ]
}

问题是,主题的类型充满正确,但其余的字段不能填补因为当后"从 d 在 q.subjectType"与 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": "Лихозвон"
        }
      ]
    }
  ]
}

抱歉我的英语很差,谷歌转换

解决方法 1:

当您使用"从"你的两个部分就会收到笛卡尔的乘法,而不是加入两个集合。 得到你想要尝试重写它像这样︰

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])))))));
官方微信
官方QQ群
31647020