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: 24 Şubat 2026 Saat 15:44 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">
|
Mesajı Yazan: M-Guney
Mesaj Tarihi: 17 Kasım 2025 Saat 11:59
{"note":"no potato","status":"Served","items":{"item_id":"2","amount":"29"}} Body yi bu şekilde yolluyorum fakat aslında {"note":"no potato","status":"Served", "items": [ { "item_id": "2", "amount": "2" }, { "item_id": "7", "amount": "1" } ] } bu şekilde yollamam gerekiyor nasıl yapabilirim.
function SetUpdateBodyV: String var Order, Items: TCLJSONObject; LPieceStr, LItemID : String; ChangeBtn : TClProButton; TempItemJson : String; FArr : TCLJSONArray; I : Integer; TempPieceLbl : TclProLabel; { Try Order = TCLJSONObject.Create; Order.AddPair('note', TCLJSONValue.Str(noteMemo.Text)); ChangeBtn = TClProButton(MainForm.Clsender); if (ChangeBtn <> nil) { Order.AddPair('status', TCLJSONValue.Str(ChangeBtn.ClTagStr)); } Items = TCLJSONObject.Create; // items{ item_id amount} TempItemJson = Clomosy.CLParseJSON(JsonResponse, 'items'); // for taking item_id FArr = TCLJSONArray.CreateFromJSON(TempItemJson); for (I=0 to FArr.Count -1){ LItemID = Clomosy.CLParseJSON(TempItemJson, IntToStr(I) + '.item_id') // item_id is ready TempPieceLbl = MainForm.clFindComponent('PieceLbl' + IntToStr(I)); // for taking amount if (TempPieceLbl <> nil) { LPieceStr = TempPieceLbl.Text; Delete(LPieceStr, 1, 1); } // amount is ready if((StrToInt(LPieceStr) <>nil)&&(StrToInt(LItemID) <>nil)){ Items.AddPair('item_id', TCLJSONValue.Str(LItemID)); Items.AddPair('amount', TCLJSONValue.Str(LPieceStr)); } }
Order.AddArray('items', Items); Clomosy.SetClipboard(Order.ToJSONString); ShowMessage('SetUpdateBodyV OUTPUT: '+Order.ToJSONString); Result = Order.ToJSONString; except ShowMessage('CalculateTotalPrice 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: M-Guney
Mesaj Tarihi: 17 Kasım 2025 Saat 13:19
|
function SetUpdateBodyV: String var Order, Items: TCLJSONObject; LPieceStr, LItemID : String; ChangeBtn : TClProButton; TempItemJson : String; FArr : TCLJSONArray; Item : TClProObject; I : Integer; TempPieceLbl : TclProLabel; { Try Order = TCLJSONObject.Create; Order.AddPair('note', TCLJSONValue.Str(noteMemo.Text)); ChangeBtn = TClProButton(MainForm.Clsender); if (ChangeBtn <> nil) { Order.AddPair('status', TCLJSONValue.Str(ChangeBtn.ClTagStr)); } Items = TCLJSONArray.Create; // items{ item_id amount} Item = TCLJSONObject.Create; TempItemJson = Clomosy.CLParseJSON(JsonResponse, 'items'); // for taking item_id FArr = TCLJSONArray.CreateFromJSON(TempItemJson); for (I=0 to FArr.Count -1){ LItemID = Clomosy.CLParseJSON(TempItemJson, IntToStr(I) + '.item_id') // item_id is ready TempPieceLbl = MainForm.clFindComponent('PieceLbl' + IntToStr(I)); // for taking amount if (TempPieceLbl <> nil) { LPieceStr = TempPieceLbl.Text; Delete(LPieceStr, 1, 1); } // amount is ready if((StrToInt(LPieceStr) <>nil)&&(StrToInt(LItemID) <>nil)){ Item.AddPair('item_id', TCLJSONValue.Str(LItemID)); Item.AddPair('amount', TCLJSONValue.Str(LPieceStr)); Items.AddObject('items',Item); ShowMessage(Item.ToJSONString+'added') } }
Order.AddArray('items', Items); Clomosy.SetClipboard(Order.ToJSONString); ShowMessage('SetUpdateBodyV OUTPUT: '+Order.ToJSONString); Result = Order.ToJSONString; except ShowMessage('CalculateTotalPrice hatası: ' + LastExceptionMessage); } }
Bu şekilde bir yol izledim yapmak için obje, array, obje şeklinde iç içe bir yapı kurguladım fakat arraya eklemek istediğim objeti eklemiyor. 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: 17 Kasım 2025 Saat 18:21
|
Her döngü de yeni Item oluşturup deneyelim
function SetUpdateBodyV: String var Order: TCLJSONObject; Items: TCLJSONArray; Item: TCLJSONObject; LPieceStr: String; LItemID: String; ChangeBtn: TClProButton; TempItemJson: String; FArr: TCLJSONArray; I: Integer; TempPieceLbl: TclProLabel; ItemAmount: Integer; ItemIDInt: Integer; { Try Order = TCLJSONObject.Create; Order.AddPair('note', TCLJSONValue.Str(noteMemo.Text)); ChangeBtn = TClProButton(MainForm.ClSender); if (ChangeBtn <> nil) { Order.AddPair('status', TCLJSONValue.Str(ChangeBtn.ClTagStr)); } Items = TCLJSONArray.Create; TempItemJson = Clomosy.CLParseJSON(JsonResponse, 'items'); if (TempItemJson == '') { Order.AddArray('items', Items); Result = Order.ToJSONString; Items.Free; Order.Free; Exit; } FArr = TCLJSONArray.Create; FArr = TCLJSONArray.CreateFromJSON(TempItemJson); if ((FArr == nil) || (FArr.Count == 0)) { Order.AddArray('items', Items); Result = Order.ToJSONString; Items.Free; Order.Free; if (FArr <> nil) { FArr.Free; } Exit; } for (I = 0 to FArr.Count - 1) { Try { LItemID = Clomosy.CLParseJSON(TempItemJson, IntToStr(I) + '.item_id'); LPieceStr = ''; TempPieceLbl = MainForm.clFindComponent('PieceLbl' + IntToStr(I)) as TclProLabel; if (TempPieceLbl <> nil) { LPieceStr = TempPieceLbl.Text; if (Length(LPieceStr) > 0) { Delete(LPieceStr, 1, 1); } } ItemAmount = 0; ItemIDInt = 0; Try if (LPieceStr <> '') { ItemAmount = StrToInt(LPieceStr); } except ShowMessage('Amount geçersiz (Index ' + IntToStr(I) + '): ' + LPieceStr); Continue; } Try if (LItemID <> '') { ItemIDInt = StrToInt(LItemID); } except ShowMessage(' Item ID geçersiz (Index ' + IntToStr(I) + '): ' + LItemID); Continue; } if ((ItemAmount > 0) && (ItemIDInt > 0)) { Item = TCLJSONObject.Create; Item.AddPair('item_id', TCLJSONValue.Str(LItemID)); Item.AddPair('amount', TCLJSONValue.Str(LPieceStr)); Items.Add(Item); ShowMessage(' Item eklendi: item_id=' + LItemID + ', amount=' + LPieceStr); } else { ShowMessage('️ UYARI: Geçersiz değerler (Index ' + IntToStr(I) + '): ItemID=' + LItemID + ', Amount=' + LPieceStr); } except ShowMessage(' Item ekleme hatası (Index ' + IntToStr(I) + '): ' + LastExceptionMessage); } } Order.AddArray('items', Items); Clomosy.SetClipboard(Order.ToJSONString); ShowMessage(' SetUpdateBodyV OUTPUT: ' + Order.ToJSONString); Result = Order.ToJSONString; if (FArr <> nil) { FArr.Free; FArr = nil; } } except ShowMessage(' SetUpdateBodyV hatası: ' + LastExceptionMessage); Result = ''; } } 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">
|
|