Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Actuaal
  12. {
  13. class JsonFileModel {
  14. public int Id { get; set; }
  15. public string Name { get; set; }
  16. public string Description { get; set; }
  17. public string RefType { get; set; }
  18. public int? ParentId { get; set; }
  19. public int? Level { get; set; }
  20. }
  21. class Program
  22. {
  23. static void WriteJsonToFile(string fileName, JObject json) {
  24. File.Create($@"c:\JsonTest\{fileName}.json").Close();
  25.  
  26. using (StreamWriter file = File.CreateText($@"c:\JsonTest\{fileName}.json"))
  27. using (JsonTextWriter writer = new JsonTextWriter(file))
  28. {
  29. json.WriteTo(writer);
  30. }
  31. Debug.WriteLine($@"Successfully parsed and created c:\JsonTest\{fileName}.json");
  32. }
  33.  
  34. static void Main(string[] args)
  35. {
  36. using (var mws = new MyWorkSearch()) {
  37. var lh = mws.WA_LearningProgrammes_LearningHierarchy
  38. .Where(x => x.RefType == "LearningFramework").ToList();
  39.  
  40. foreach (var i in lh) {
  41. JObject rootLevel = new JObject(
  42. new JProperty("Id", i.Id),
  43. new JProperty("Name", i.Name),
  44. new JProperty("Description", i.Description),
  45. new JProperty("RefType", i.RefType),
  46. new JProperty("Level", i.Level)
  47. );
  48. var children = FindById(i.Id);
  49. rootLevel.Add("Children", JsonConvert.SerializeObject(children));
  50. WriteJsonToFile("1test", rootLevel);
  51. }
  52. }
  53. }
  54.  
  55. //find children
  56. static List<WA_LearningProgrammes_LearningHierarchy> FindById(int? Id) {
  57. List<WA_LearningProgrammes_LearningHierarchy> retValue = null;
  58. using (var mws = new MyWorkSearch()) {
  59. var children = mws.WA_LearningProgrammes_LearningHierarchy
  60. .Where(x => x.ParentId == Id)
  61. .ToList();
  62. retValue = children;
  63. }
  64. return retValue;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement