Sayfayı Yazdır | Pencereyi Kapat

ClRest response değişken kullanımı

Nereden Yazdırıldığı: Clomosy | Forum
Kategori: Genel Programlama
Forum Adı: Clomosy ile değişken kullanımı
Forum Tanımlaması: TRObject dili ile değişken tanımlaması ve ekranda gösterme
URL: https://forum.clomosy.com.tr/forum_posts.asp?TID=1215
Tarih: 06 Kasım 2025 Saat 16:21
Program Versiyonu: Web Wiz Forums 12.07 - https://www.webwizforums.com


Konu: ClRest response değişken kullanımı
Mesajı Yazan: M-Guney
Konu: ClRest response değişken kullanımı
Mesaj Tarihi: 06 Kasım 2025 Saat 10:14
ClRest ile Api den aldığım veriyi bir objeye atayım sonra o objedeki belirli verileri shift_name'i alıp combo box a eklemek istiyorum.

void GetShiftTypes
var
 Obj: TCLJSONObject;
{
  try
    clRest.BaseURL = 'http://BENIM_UZANTIM/shifttypes';
    clRest.Accept = 'application/json';
    clRest.Method = rmGET;
    
    clRest.Execute;
    ShowMessage(clRest.Response);
    ///FirstPlayer = Clomosy.CLParseJSON(Players, 'categories.0.pairs.' + IntToStr(RandomIndex) + '.0');
    Obj=clRest.Response;
    ShowMessage(obj.ToJSONString);
    //ShowMessage(Obj.ToJSONString); // Hata ayıklamayı türkçeleştirebilirsin
    //Combo1.AddItem(Obj.shift_id, Obj.shift_name);
  
    
  except
  ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }

Örnek json çıktısı

 {
        "shift_id": 2,
        "shift_name": "Sabahçı",
        "user_id": null,
        "start_time": "1970-01-01T09:00:00.000Z",
        "end_time": "1970-01-01T18:30:00.000Z",
        "expenses": null
  },

https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" rel="nofollow - https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28oN72PdrCzSjY4U6VaAw1EQ==" data-cf-beacon="{"version":"2024.11.0","token":"439455f3e46c40b98dbd42a2f1a954d8","r":1,"server_timing":{"name":{"cfCacheStatus":true,"cfEdge":true,"cfExtPri":true,"cfL4":true,"cfOrigin":true,"cfSpeedBrain":true},"location_startswith":null}}" crossorigin="anonymous">



Cevaplar:
Mesajı Yazan: Emr.Erkmn
Mesaj Tarihi: 06 Kasım 2025 Saat 10:58
Merhaba Güney, 
https://www.docs.clomosy.com/CLParseJSON" rel="nofollow - https://www.docs.clomosy.com/CLParseJSON gelen cevabı ile ayrıştırdım, 
obj= clRest.Response kodu çalışmaz çünkü response bir string bu yüzden JSON parse etmek gerekiyor. 
GetShiftTypes da API'ye get isteğini gönderdik (async ile)
GetShiftTypesCompleted ile response geldiğin de çağırdık ve 
Json response pars ettik
array içinde ki her eleman için shift_name değerini alıp comboboxa ekledik.

Kendi basuURL API'ni değiştirip dener misin? 

var
  MainForm: TClForm;
  clRest: TclRest;
  Combo1: TCLComboBox;
  GetShiftBtn: TClProButton;

void GetShiftTypesCompleted
var
  jsonResponse: String;
  shiftName: String;
  shiftId: String;
  i: Integer;
{
  Try
  
    jsonResponse = clRest.Response;
    
    // ComboBox'ı temizle
    Combo1.Items.Clear;
    
    // Array içindeki her eleman için shift_name'i al ve ComboBox'a ekle
    i = 0;
    while (True)
    {
      // Array elemanlarına erişim: "0.shift_name", "1.shift_name", vb.
      shiftName = Clomosy.CLParseJSON(jsonResponse, IntToStr(i) + '.shift_name');
      
      if (shiftName == '')
      {
        break;
      }
      
      // shift_id'yi de al (isteğe bağlı)
      shiftId = Clomosy.CLParseJSON(jsonResponse, IntToStr(i) + '.shift_id');
      
      // ComboBox'a shift_name'i ekle
      Combo1.Items.Add(shiftName);
      
      i = i + 1;
      
    }
    
    if (i > 0)
    {
      ShowMessage(IntToStr(i) + ' vardiya tipi başarıyla yüklendi.');
    }
    else
    {
      ShowMessage('Vardiya tipi bulunamadı veya JSON formatı hatalı.');
    }
  
  except
  
    ShowMessage('Hata: ' + LastExceptionClassName + ' - ' + LastExceptionMessage);
  }
}

void GetShiftTypes
{
    clRest.BaseURL = 'http://BENIM_UZANTIM/shifttypes';
    clRest.Accept = 'application/json';
    clRest.Method = rmGET;
    clRest.OnCompleted = 'GetShiftTypesCompleted';
    
    clRest.ExecuteAsync;
}

{
  MainForm = TClForm.Create(Self);
  
  Combo1 = MainForm.AddNewComboBox(MainForm, 'Combo1');
  Combo1.Align = alTop;
  Combo1.Height = 50;
  Combo1.Margins.Top = 20;
  Combo1.Margins.Left = 20;
  Combo1.Margins.Right = 20;
  
  GetShiftBtn = MainForm.AddNewProButton(MainForm, 'GetShiftBtn', 'Vardiya Tiplerini Yükle');
  GetShiftBtn.Align = alTop;
  GetShiftBtn.Height = 50;
  GetShiftBtn.Margins.Top = 10;
  GetShiftBtn.Margins.Left = 20;
  GetShiftBtn.Margins.Right = 20;
  GetShiftBtn.clProSettings.BackgroundColor = clAlphaColor.clHexToColor('#3498DB');
  GetShiftBtn.clProSettings.FontColor = clAlphaColor.clHexToColor('#FFFFFF');
  GetShiftBtn.clProSettings.FontSize = 16;
  GetShiftBtn.clProSettings.FontVertAlign = palcenter;
  GetShiftBtn.clProSettings.FontHorzAlign = palcenter;
  GetShiftBtn.clProSettings.RoundHeight = 8;
  GetShiftBtn.clProSettings.RoundWidth = 8;
  GetShiftBtn.SetclProSettings(GetShiftBtn.clProSettings);
  MainForm.AddNewEvent(GetShiftBtn, tbeOnClick, 'GetShiftTypes');
  
  clRest = TclRest.Create;
  
  MainForm.Run;
}


https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" rel="nofollow - https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28oN72PdrCzSjY4U6VaAw1EQ==" data-cf-beacon="{"version":"2024.11.0","token":"439455f3e46c40b98dbd42a2f1a954d8","r":1,"server_timing":{"name":{"cfCacheStatus":true,"cfEdge":true,"cfExtPri":true,"cfL4":true,"cfOrigin":true,"cfSpeedBrain":true},"location_startswith":null}}" crossorigin="anonymous">
< defer="" ="https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28on72PdrCzSjY4U6VaAw1EQ==" -cf-beacon=""version":"2024.11.0","token":"439455f3e46c40b98dbd42a2f1a954d8","r":1,"_timing":"name":"cfCacheStatus":true,"cfEdge":true,"cfExtPri":true,"cfL4":true,"cfOrigin":true,"cfSpeedBrain":true,"location_startswith":null" crossorigin="anonymous">


Mesajı Yazan: M-Guney
Mesaj Tarihi: 06 Kasım 2025 Saat 14:01
void GetShiftTypesCompleted
var
  //strIncomingData, shiftName, shiftStartTime, shiftEndTime : String
  jsonResponse: String;
  shiftName: String;
  shiftId: String;
  i: Integer;
{
  try
    clRest.Execute;
    ShowMessage(clRest.Response);
    jsonResponse = clRest.Response;
    // ComboBox'ı temizle
    Combo1.Items.Clear;
    
    // Array içindeki her eleman için shift_name'i al ve ComboBox'a ekle
    i = 0;
    while (True)
    {
      // Array elemanlarına erişim: "0.shift_name", "1.shift_name", vb.
      shiftName = Clomosy.CLParseJSON(jsonResponse, IntToStr(i) + '.shift_name');
      
      if (shiftName == '')
      {
        break;
      }
      
      // shift_id'yi de al (isteğe bağlı)
      shiftId = Clomosy.CLParseJSON(jsonResponse, IntToStr(i) + '.shift_id');
      
      // ComboBox'a shift_name'i ekle
      Combo1.Items.Add(shiftName);
      ShowMessage('İ: '+IntToStr(i)+ ' shift_id: ' + IntToStr(shiftId) );
      i = i + 1;
      
      
    }
    
    if (i > 0)
    {
      ShowMessage(IntToStr(i) + ' vardiya tipi başarıyla yüklendi.');
    }
    else
    {
      ShowMessage('Vardiya tipi bulunamadı veya JSON formatı hatalı.');
    }
  
    
  except
  ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
}  

Bu şekildeyken çalışıyor fakat count ile saymam gerek sanırsam çünkü en sonunda geçersiz dizi indeksi diyor verilen jsondan fazla yere bakmaya çalışıyor

https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" rel="nofollow - https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28oN72PdrCzSjY4U6VaAw1EQ==" data-cf-beacon="{"version":"2024.11.0","token":"439455f3e46c40b98dbd42a2f1a954d8","r":1,"server_timing":{"name":{"cfCacheStatus":true,"cfEdge":true,"cfExtPri":true,"cfL4":true,"cfOrigin":true,"cfSpeedBrain":true},"location_startswith":null}}" crossorigin="anonymous">


Mesajı Yazan: Emr.Erkmn
Mesaj Tarihi: 06 Kasım 2025 Saat 14:34
Evet Güney count ile önce dizinin uzunluğunu bulup daha sonra bulunan count kadar veri eklemesi yapmamız gerekiyor. 
https://www.docs.clomosy.com/index.php?title=TclJSONArray" rel="nofollow - https://www.docs.clomosy.com/index.php?title=TclJSONArray
https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" rel="nofollow - https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28oN72PdrCzSjY4U6VaAw1EQ==" data-cf-beacon="{"version":"2024.11.0","token":"439455f3e46c40b98dbd42a2f1a954d8","r":1,"server_timing":{"name":{"cfCacheStatus":true,"cfEdge":true,"cfExtPri":true,"cfL4":true,"cfOrigin":true,"cfSpeedBrain":true},"location_startswith":null}}" crossorigin="anonymous">


Mesajı Yazan: M-Guney
Mesaj Tarihi: 06 Kasım 2025 Saat 14:48
Teşekkürler



Sayfayı Yazdır | Pencereyi Kapat

Forum Software by Web Wiz Forums® version 12.07 - https://www.webwizforums.com
Copyright ©2001-2024 Web Wiz Ltd. - https://www.webwiz.net