Guest User

FirebaseJSON

a guest
Dec 24th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. // 3251692 @ Pantip
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. using FireSharp;
  8. using FireSharp.Config;
  9. using FireSharp.Interfaces;
  10. using FireSharp.Response;
  11. using Newtonsoft.Json.Linq;
  12.  
  13. namespace FireSharpPantip_gaka
  14. {
  15.     public partial class FrmGaka : Form
  16.     {
  17.         IFirebaseConfig config = new FirebaseConfig {
  18.             AuthSecret = "your_firebase_secret",
  19.             // เปิดเป็น Test Mode
  20.             BasePath = "https://pantip-gaka.firebaseio.com/"
  21.         };
  22.         List<string> DName = new List<string>();
  23.         List<string> DValue = new List<string>();
  24.         List<string> DPath = new List<string>();
  25.         public FrmGaka()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         //ถ้าต้องการใช้คลาส ลบการประกาศ List<string> ทั้งสามด้านบน แล้วเปลี่ยนด้านล่าง
  31.         //  DName => data.Name
  32.         //  DValue => data.Value
  33.         //  DPath => data.Path
  34.         //และ uncomment คลาส ValueCollection บรรทัด 33,99-104
  35.         //ValueCollection data = new ValueCollection();
  36.         private void FrmGaka_Load(object sender, EventArgs e)
  37.         {
  38.             IFirebaseClient client = new FirebaseClient(config);
  39.                
  40.             FirebaseResponse response = client.Get("");
  41.             JObject o = JObject.Parse(response.Body);
  42.  
  43.             AddValtoList(o);
  44.            
  45.             string checkdata = "";
  46.             for (int i = 0; i < DName.Count; i++)
  47.             {
  48.                 checkdata += String.Format("{0}. {1} : {2} ({3})\n", i+1, DName[i], DValue[i], DPath[i]);                
  49.             }
  50.             MessageBox.Show(checkdata);
  51.         }
  52.  
  53.         private void AddVal(string n, string v, string p)
  54.         {            
  55.             DName.Add(n);
  56.             DValue.Add(v);
  57.             DPath.Add(p);
  58.         }
  59.  
  60.         private void AddValtoList(JObject o)
  61.         {
  62.             List<string> l1 = o.Properties().Select(p => p.Name).ToList();
  63.             foreach (string n1 in l1)
  64.             {
  65.                 //List<string> l3 = ((JObject)o[n1]["Task"]).Properties().Select(p => p.Name).ToList();
  66.                 foreach (JProperty n3 in o[n1]["Task"])
  67.                 {
  68.                     foreach (JProperty n4 in o[n1]["Task"][n3.Name])
  69.                     {
  70.                         foreach (JProperty n5 in o[n1]["Task"][n3.Name][n4.Name])
  71.                         {
  72.                             if (n5.Value.Type.ToString() != "String")
  73.                             {
  74.                                 foreach (JProperty n6 in o[n1]["Task"][n3.Name][n4.Name][n5.Name])
  75.                                 {
  76.                                     if (n6.Value.Type.ToString() != "String")
  77.                                     {
  78.                                         foreach (JProperty n7 in o[n1]["Task"][n3.Name][n4.Name][n5.Name][n6.Name])
  79.                                         {
  80.                                             AddVal(n7.Name, (string)n7.Value, n7.Path);
  81.                                         }
  82.                                     }
  83.                                     else
  84.                                     {
  85.                                         AddVal(n6.Name, (string)n6.Value, n6.Path);
  86.                                     }
  87.                                 }
  88.                             }
  89.                             else
  90.                             {
  91.                                 AddVal(n5.Name, (string)n5.Value, n5.Path);
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.  
  97.             }
  98.         }
  99.     }
  100.  
  101.     //class ValueCollection
  102.     //{
  103.     //    public List<string> Name { get; set; } = new List<string>();
  104.     //    public List<string> Value { get; set; } = new List<string>();
  105.     //    public List<string> Path { get; set; } = new List<string>();
  106.     //}
  107. }
Advertisement
Add Comment
Please, Sign In to add comment