Sayfayı Yazdır | Pencereyi Kapat

JsonArray 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=1227
Tarih: 13 Kasım 2025 Saat 22:35
Program Versiyonu: Web Wiz Forums 12.07 - https://www.webwizforums.com


Konu: JsonArray kullanımı
Mesajı Yazan: M-Guney
Konu: JsonArray kullanımı
Mesaj Tarihi: 13 Kasım 2025 Saat 17:44
Burada bulunan jsondaki itemname i almak istiyorum ve jsonarraya json ile atama yapmak istiyorum.

Örnek olarak çalıştıramadığım kod

var
  Arr : TCLJSONArray;
{
Arr = TCLJSONArray.Create;
Arr = TCLJSONArray.CreateFromJSON('{"order_id": 30,"table_name": "K12","order_time": "17:57    13.11.2025","status": "Pending","note": "Please make one of the kebabs extra spicy.","grand_total": 398,"items": [{"item_id": 2,"item_name": "kebap","amount": 2,"unit_price": 123,"total_price": 246},{"item_id": 4,"item_name": "Izgara piliç","amount": 1,"unit_price": 76,"total_price": 76},{"item_id": 7,"item_name": "Izgara kanat","amount": 1,"unit_price": 76,"total_price": 76}]}');


ShowMessage(Arr.ToJSONString);
  
}


Asıl yapmak istediğim ise Burada invalid type cast hatasını gidermek istiyorum.

void GetOrderDetailsCompletedV(Sender)
var
  Arr : TCLJSONArray;
  restObj: TCLRest;
  responseLength: Integer;
  FQry: TCLJSONQuery;
{
  Try
  
    restObj = TCLRest(Sender);
    
    if (restObj == nil)
    {
      ShowMessage(' HATA: Sender nil! Response alınamadı!');
      Exit;
    }
    
    JsonResponse = restObj.Response;
    
    responseLength = Length(JsonResponse);
    if (responseLength == 0)
    {
      ShowMessage('️ UYARI: Response boş! (0 karakter)');
      ShowMessage('query is Served (boş response)');
      Exit;  
    }/*
    If (responseLength > 50){
       jsonOutput = Copy(JsonResponse, 1, 50) + '...' + JsonResponse;
      
    }
    ShowMessage(' restObj Response: ' + jsonOutput);*/
    FQry = TCLJSONQuery.Create(nil); 
    FQry = Clomosy.ClDataSetFromJSON(JsonResponse);
    
    Arr = TCLJSONArray.Create;
    Try
      Arr = TCLJSONArray.CreateFromJSON(JsonResponse);
      ShowMessage(JsonResponse);
      ShowMessage(Arr.ToJSONString);
      
      if ((Arr == nil) || (Arr.Count == 0))
      {
        ShowMessage('JSON Array boş!');
        ShowMessage('query is Served (boş array)');
        Exit;
      }
      
      noteMemo.Text = Clomosy.CLParseJSON(JsonResponse, '0.note');
      AmountValueLbl.Text = Clomosy.CLParseJSON(JsonResponse, '0.grand_total') + 'TL';
      DisplayArrivalTime.Text = Clomosy.CLParseJSON(JsonResponse, '0.order_time');
      DetailsPnlV(FQry);
    finally
      if (Arr <> nil)
      {
        Arr.Free;
        Arr = nil;
      }
    
    
    //ShowMessage(' query is Served (başarılı)');
  }
  except
    ShowMessage(' Hata: ' + LastExceptionMessage);
  }
}
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: 13 Kasım 2025 Saat 17:52
void GetItemsFromJSON
var
  jsonString: String;
  itemsJson: String;
  i: Integer;
  itemName: String;
{
  Try
    jsonString = '{"order_id": 30,"table_name": "K12","order_time": "17:57    13.11.2025","status": "Pending","note": "Please make one of the kebabs extra spicy.","grand_total": 398,"items": [{"item_id": 2,"item_name": "kebap","amount": 2,"unit_price": 123,"total_price": 246},{"item_id": 4,"item_name": "Izgara piliç","amount": 1,"unit_price": 76,"total_price": 76},{"item_id": 7,"item_name": "Izgara kanat","amount": 1,"unit_price": 76,"total_price": 76}]}';
    
    // yöntem: CLParseJSON ile items array'ini al
    itemsJson = Clomosy.CLParseJSON(jsonString, 'items');
    
    if (itemsJson <> '')
    {
      ItemsArr = TCLJSONArray.Create;
      ItemsArr = TCLJSONArray.CreateFromJSON(itemsJson);
      
      ShowMessage('Items Array Count: ' + IntToStr(ItemsArr.Count));
      
      for (i = 0 to ItemsArr.Count - 1)
      {
        itemName = Clomosy.CLParseJSON(itemsJson, IntToStr(i) + '.item_name');
        ShowMessage('Item ' + IntToStr(i) + ': ' + itemName);
      }
      
      ItemsArr.Free;
      ItemsArr = nil;
    }
  except
    ShowMessage('GetItemsFromJSON hatası: ' + LastExceptionMessage);
  }
}
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: 13 Kasım 2025 Saat 17:55
void GetOrderDetailsCompletedV(Sender)
var
  restObj: TCLRest;
  responseLength: Integer;
  itemsJson: String;
  i: Integer;
  itemName: String;
{
  Try
    restObj = TCLRest(Sender);
    
    if (restObj == nil)
    {
      ShowMessage('Sender nil Response alınamadı');
      Exit;
    }
    
    JsonResponse = restObj.Response;
    
    responseLength = Length(JsonResponse);
    if (responseLength == 0)
    {
      ShowMessage('UYARI: Response boş ');
      Exit;
    }
    
    FQry = Clomosy.ClDataSetFromJSON(JsonResponse);
    
    itemsJson = Clomosy.CLParseJSON(JsonResponse, 'items');
    
    if (itemsJson == '')
    {
      ShowMessage('UYARI: Items array bulunamadı');
      Exit;
    }
    
    Arr = TCLJSONArray.Create;
    Arr = TCLJSONArray.CreateFromJSON(itemsJson);
    
    if ((Arr == nil) || (Arr.Count == 0))
    {
      ShowMessage('UYARI: Items array boş');
      if (Arr <> nil)
      {
        Arr.Free;
        Arr = nil;
      }
      Exit;
    }
    
    for (i = 0 to Arr.Count - 1)
    {
      itemName = Clomosy.CLParseJSON(itemsJson, IntToStr(i) + '.item_name');
      ShowMessage('Item ' + IntToStr(i) + ': ' + itemName);
    }
    
    noteMemo.Text = Clomosy.CLParseJSON(JsonResponse, 'note');
    AmountValueLbl.Text = Clomosy.CLParseJSON(JsonResponse, 'grand_total') + 'TL';
    DisplayArrivalTime.Text = Clomosy.CLParseJSON(JsonResponse, 'order_time');
    
    DetailsPnlV(FQry);
    
    if (Arr <> nil)
    {
      Arr.Free;
      Arr = nil;
    }
    
    if (FQry <> nil)
    {
      FQry.Free;
      FQry = nil;
    }
  except
    ShowMessage('GetOrderDetailsCompletedV hatası: ' + LastExceptionMessage);
    
    if (Arr <> nil)
    {
      Arr.Free;
      Arr = nil;
    }
    
    if (FQry <> nil)
    {
      FQry.Free;
      FQry = nil;
    }
  }
}
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">



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