Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
json yapma oluşturma, nasıl olur
#1
Question 
iyi akşamlar kolay gelsin herkese.

elimde  örnek bir  json dosyası var bende bunun aynısını yapmak zorundayım. içerisinde json array var, bende bu kısmın içinden çıkamadım, yardımınızı rica ediyorum.

bu yapıya sahip bir json dosyasını nasıl oluşturabilirim. veriler adodataset den gelecek. sanırım onları adodataset.............AsString; diye alabilirim.

{
  "data": {
    "cloudcover": "0",
    "FeelsLikeC": "-9",
    "FeelsLikeF": "15",
    "humidity": "93",
    "observation_time": "04:10 AM",
    "precipMM": "0.0",
    "pressure": "1007",
    "temp_C": "-6",
    "temp_F": "21",
    "visibility": "10",
    "weatherCode": "113",
    "weatherDesc": [],
    "weatherIconUrl": [],
    "winddir16Point": "SE",
    "winddirDegree": "130",
    "windspeedKmph": "7",
    "windspeedMiles": "4"
  }
}
Cevapla
#2
USES System.JSON

var
JSONArray: TJSONArray;
jsonObject, jsonTablo: TJSONObject;
begin
JSONArray := TJSONArray.Create;
jsonObject := TJSONObject.Create;
jsonTablo := TJSONObject.Create;

jsonObject.AddPair(TJSONPair.Create('Cloudcover', '0'));
jsonObject.AddPair(TJSONPair.Create('FeelsLikeC', '-9'));
JSONArray.AddElement(jsonObject);

jsonTablo.AddPair('data', JSONArray);
Memo1.Lines.Add(jsonTablo.ToString);
end;
Cevapla
#3
(03-05-2021, Saat: 04:00)yanniosman Adlı Kullanıcıdan Alıntı: USES System.JSON

var
 JSONArray: TJSONArray;
 jsonObject, jsonTablo: TJSONObject;
begin
 JSONArray := TJSONArray.Create;
 jsonObject := TJSONObject.Create;
 jsonTablo := TJSONObject.Create;

 jsonObject.AddPair(TJSONPair.Create('Cloudcover', '0'));
 jsonObject.AddPair(TJSONPair.Create('FeelsLikeC', '-9'));
 JSONArray.AddElement(jsonObject);

 jsonTablo.AddPair('data', JSONArray);
 Memo1.Lines.Add(jsonTablo.ToString);
end;

Merhaba dostum,  düz json oluşturabiliyorum ama, şu iç içe geçmiş yapıyı kuramadım. o konuda küçük bir örnek verebilirmisin.

https://resmim.net/zojjqg/ resmideki gibi
Cevapla
#4
(03-05-2021, Saat: 15:09)noname101 Adlı Kullanıcıdan Alıntı:
(03-05-2021, Saat: 04:00)yanniosman Adlı Kullanıcıdan Alıntı: USES System.JSON

var
 JSONArray: TJSONArray;
 jsonObject, jsonTablo: TJSONObject;
begin
 JSONArray := TJSONArray.Create;
 jsonObject := TJSONObject.Create;
 jsonTablo := TJSONObject.Create;

 jsonObject.AddPair(TJSONPair.Create('Cloudcover', '0'));
 jsonObject.AddPair(TJSONPair.Create('FeelsLikeC', '-9'));
 JSONArray.AddElement(jsonObject);

 jsonTablo.AddPair('data', JSONArray);
 Memo1.Lines.Add(jsonTablo.ToString);
end;

Merhaba dostum,  düz json oluşturabiliyorum ama, şu iç içe geçmiş yapıyı kuramadım. o konuda küçük bir örnek verebilirmisin.

https://resmim.net/zojjqg/   resmideki gibi


var
  JSONArray0, JSONArray0Variant: TJSONArray;
  jsonObject0, jsonObject0Variant, jsonProduct, jsonProduct0,
    jsonProduct0Variant: TJSONObject;
begin
  JSONArray0 := TJSONArray.Create;
  JSONArray0Variant := TJSONArray.Create;

  jsonObject0 := TJSONObject.Create;
  jsonObject0Variant := TJSONObject.Create;

  jsonProduct := TJSONObject.Create; // products
  jsonProduct0 := TJSONObject.Create; // 0
  jsonProduct0Variant := TJSONObject.Create; // Variants

{$REGION '0'}
  jsonObject0.AddPair(TJSONPair.Create('id', '6595121217702'));
  jsonObject0.AddPair(TJSONPair.Create('title', '0-3 Ay bod..'));
  JSONArray0.AddElement(jsonObject0);
  jsonProduct0.AddPair('0', JSONArray0); // 0 Oluşturuyoruz
{$ENDREGION}

{$REGION '0 > Variants'}
  jsonObject0Variant.AddPair(TJSONPair.Create('id', '396441...'));
  jsonObject0Variant.AddPair(TJSONPair.Create('title', 'Default Title'));
  JSONArray0Variant.AddElement(jsonObject0);
  jsonProduct0.AddPair('variants', JSONArray0Variant); // Variants > 0 ekliyoruz
{$ENDREGION}

  jsonProduct.AddPair('products', jsonProduct0); // 0 > product ekliyoruz

  Memo1.Lines.Add(jsonProduct.ToString);
end;


Bir üstte verdiğim örnekten tek farkı iç içe JsonArray eklenmiş olması. Basitçe mantığı anlatmak için düz yazı gibi kodladım kusura bakmayın.
Cevapla
#5
(03-05-2021, Saat: 18:23)yanniosman Adlı Kullanıcıdan Alıntı:
(03-05-2021, Saat: 15:09)noname101 Adlı Kullanıcıdan Alıntı: Merhaba dostum,  düz json oluşturabiliyorum ama, şu iç içe geçmiş yapıyı kuramadım. o konuda küçük bir örnek verebilirmisin.

https://resmim.net/zojjqg/   resmideki gibi


var
  JSONArray0, JSONArray0Variant: TJSONArray;
  jsonObject0, jsonObject0Variant, jsonProduct, jsonProduct0,
    jsonProduct0Variant: TJSONObject;
begin
  JSONArray0 := TJSONArray.Create;
  JSONArray0Variant := TJSONArray.Create;

  jsonObject0 := TJSONObject.Create;
  jsonObject0Variant := TJSONObject.Create;

  jsonProduct := TJSONObject.Create; // products
  jsonProduct0 := TJSONObject.Create; // 0
  jsonProduct0Variant := TJSONObject.Create; // Variants

{$REGION '0'}
  jsonObject0.AddPair(TJSONPair.Create('id', '6595121217702'));
  jsonObject0.AddPair(TJSONPair.Create('title', '0-3 Ay bod..'));
  JSONArray0.AddElement(jsonObject0);
  jsonProduct0.AddPair('0', JSONArray0); // 0 Oluşturuyoruz
{$ENDREGION}

{$REGION '0 > Variants'}
  jsonObject0Variant.AddPair(TJSONPair.Create('id', '396441...'));
  jsonObject0Variant.AddPair(TJSONPair.Create('title', 'Default Title'));
  JSONArray0Variant.AddElement(jsonObject0);
  jsonProduct0.AddPair('variants', JSONArray0Variant); // Variants > 0 ekliyoruz
{$ENDREGION}

  jsonProduct.AddPair('products', jsonProduct0); // 0 > product ekliyoruz

  Memo1.Lines.Add(jsonProduct.ToString);
end;


Bir üstte verdiğim örnekten tek farkı iç içe JsonArray eklenmiş olması. Basitçe mantığı anlatmak için düz yazı gibi kodladım kusura bakmayın.


Ustam hakkını helal et, anlatımın çok güzel sadece ben anlamadım tam mantığını, çıkamadım işin içinden,  hem aray hem de normal yazımlar ikisi bir nasıl olacak, neyi nasıl yazarsam nerede konumlanır anlayamadım.

attığım resimde de anlatmaya çalıştım,  "products" ın hemen altında çıkması gerekenler var, sizin ikinci örnekte onlarda bir array içinde sanırım, onlar direk açıkta olması altına bir aray hatta aray içinde ikinci bir aray şeklinde yapı var (sağdaki resimde), yanı kısacası ne yazınca nerede çıkıyor inanın anlamadım( sorun ben kaynaklı Smile sizlik değil ),  bir türlü istediğim formasyona sokamadım.

https://resimlink.com/PHQZ7


{
 "products": [
   {
     "id": 6595121217702,
     "title": "0-3 Ay Body, Battaniye Set",
     "published_at": "2021-03-15T11:58:23+03:00",
     "status": "active",
     "published_scope": "global",
     "tags": " İndirim Setleri",
     "admin_graphql_api_id": "/Product/6595121217702",
     "variants": [
       {
         "id": 39644128051366,
         "product_id": 6595121217702,
         "title": "Default Title",
         "option1": "Default Title",
         "updated_at": "2021-04-01T14:34:43+03:00",
         "image_id": null,
         "weight_unit": "kg",
         "inventory_item_id": 41738310058150,
         "inventory_quantity": 7,
         "old_inventory_quantity": 7,
         "requires_shipping": true,
         "admin_graphql_api_id": "gid://39644128051366"
       }
     ],
     "options": [
       {
         "id": 8479576260774,
         "product_id": 6595121217702,
         "name": "Title",
         "position": 1,
         "values": [
           "Default Title"
         ]
       }
     ]
   }
 ]
}
Cevapla
#6
En kolay ve en yeni teknikle anlatayım.

TJsonObjectWriter sınıfı ile kolayca, komplex JSON verileri oluşturabilirsiniz.
Yazdığınız örnek JSON verisinden yola çıkarak, bu veriyi nasıl oluşturabileceğinizi göstereyim.

Uses System.JSON.Writers, System.JSON;

var
 wrtJSON: TJsonObjectWriter;
begin
 wrtJSON := TJsonObjectWriter.Create();
 try
   wrtJSON.WriteStartObject;
     wrtJSON.WritePropertyName('products');
        wrtJSON.WriteStartArray;
           wrtJSON.WriteStartObject;
               wrtJSON.WritePropertyName('id');
               wrtJSON.WriteValue(6595121217702);
               wrtJSON.WritePropertyName('title');
               wrtJSON.WriteValue('0-3 Ay Body, Battaniye Set');
               wrtJSON.WritePropertyName('published_at');
               wrtJSON.WriteValue(DateTimeToStr(Now));
               wrtJSON.WritePropertyName('status');
               wrtJSON.WriteValue('active');
               wrtJSON.WritePropertyName('published_scope');
               wrtJSON.WriteValue('global');
               wrtJSON.WritePropertyName('tags');
               wrtJSON.WriteValue('İndirim Setleri');
               wrtJSON.WritePropertyName('admin_graphql_api_id');
               wrtJSON.WriteValue('/Product/6595121217702');
               wrtJSON.WritePropertyName('variants');
                  wrtJSON.WriteStartArray;
                     wrtJSON.WriteStartObject;
                        wrtJSON.WritePropertyName('id');
                        wrtJSON.WriteValue(39644128051366);
                        wrtJSON.WritePropertyName('product_id');
                        wrtJSON.WriteValue(6595121217702);
                        wrtJSON.WritePropertyName('title');
                        wrtJSON.WriteValue('Default Title');
                        wrtJSON.WritePropertyName('option1');
                        wrtJSON.WriteValue('Default Title');
                        wrtJSON.WritePropertyName('updated_at');
                        wrtJSON.WriteValue(DateTimeToStr(Now));
                        wrtJSON.WritePropertyName('image_id');
                        wrtJSON.WriteValue('null');
                        wrtJSON.WritePropertyName('weight_unit');
                        wrtJSON.WriteValue('kg');
                        wrtJSON.WritePropertyName('inventory_item_id');
                        wrtJSON.WriteValue(41738310058150);
                        wrtJSON.WritePropertyName('inventory_quantity');
                        wrtJSON.WriteValue(7);
                        wrtJSON.WritePropertyName('old_inventory_quantity');
                        wrtJSON.WriteValue(7);
                        wrtJSON.WritePropertyName('requires_shipping');
                        wrtJSON.WriteValue(true);
                        wrtJSON.WritePropertyName('admin_graphql_api_id');
                        wrtJSON.WriteValue('gid://39644128051366');
                     wrtJSON.WriteEndObject;
                  wrtJSON.WriteEndArray;
                  wrtJSON.WritePropertyName('options');
                  wrtJSON.WriteStartArray;
                     wrtJSON.WriteStartObject;
                        wrtJSON.WritePropertyName('id');
                        wrtJSON.WriteValue(8479576260774);
                        wrtJSON.WritePropertyName('product_id');
                        wrtJSON.WriteValue(6595121217702);
                        wrtJSON.WritePropertyName('name');
                        wrtJSON.WriteValue('Title');
                        wrtJSON.WritePropertyName('position');
                        wrtJSON.WriteValue(1);
                        wrtJSON.WritePropertyName('values');
                        wrtJSON.WriteStartArray;
                           wrtJSON.WriteValue('Default Title');
                        wrtJSON.WriteEndArray;
                     wrtJSON.WriteEndObject;
                  wrtJSON.WriteEndArray;
               wrtJSON.WriteEndObject;
           wrtJSON.WriteEndArray;
   wrtJSON.WriteEndObject;
   Memo1.Text := wrtJSON.JSON.Format;
 finally
   wrtJSON.DisposeOf;
 end;
end;

Ekran görüntüsü
   

Gayet kolay ve açıklayıcı olduğunu düşünüyorum. Sizce?
Begin : = end / 2;
Cevapla
#7
İşte olay budur :-) Bende Delphi'de çok yeniyim, güzel kaynak oldu. Ellerinize sağlık, çok teşekkür ederim.
Cevapla
#8
Şimdi fark ettim. 
wrtJSON.WriteValue('null');
satırı yerine
wrtJSON.WriteNull;
yazmalısınız.
Begin : = end / 2;
Cevapla
#9
(03-05-2021, Saat: 23:27)RAD Coder Adlı Kullanıcıdan Alıntı: En kolay ve en yeni teknikle anlatayım.

TJsonObjectWriter sınıfı ile kolayca, komplex JSON verileri oluşturabilirsiniz.
Yazdığınız örnek JSON verisinden yola çıkarak, bu veriyi nasıl oluşturabileceğinizi göstereyim.

Uses System.JSON.Writers, System.JSON;

var
 wrtJSON: TJsonObjectWriter;
begin
 wrtJSON := TJsonObjectWriter.Create();
 try
   wrtJSON.WriteStartObject;
     wrtJSON.WritePropertyName('products');
        wrtJSON.WriteStartArray;
           wrtJSON.WriteStartObject;
               wrtJSON.WritePropertyName('id');
               wrtJSON.WriteValue(6595121217702);
               wrtJSON.WritePropertyName('title');
               wrtJSON.WriteValue('0-3 Ay Body, Battaniye Set');
               wrtJSON.WritePropertyName('published_at');
               wrtJSON.WriteValue(DateTimeToStr(Now));
               wrtJSON.WritePropertyName('status');
               wrtJSON.WriteValue('active');
               wrtJSON.WritePropertyName('published_scope');
               wrtJSON.WriteValue('global');
               wrtJSON.WritePropertyName('tags');
               wrtJSON.WriteValue('İndirim Setleri');
               wrtJSON.WritePropertyName('admin_graphql_api_id');
               wrtJSON.WriteValue('/Product/6595121217702');
               wrtJSON.WritePropertyName('variants');
                  wrtJSON.WriteStartArray;
                     wrtJSON.WriteStartObject;
                        wrtJSON.WritePropertyName('id');
                        wrtJSON.WriteValue(39644128051366);
                        wrtJSON.WritePropertyName('product_id');
                        wrtJSON.WriteValue(6595121217702);
                        wrtJSON.WritePropertyName('title');
                        wrtJSON.WriteValue('Default Title');
                        wrtJSON.WritePropertyName('option1');
                        wrtJSON.WriteValue('Default Title');
                        wrtJSON.WritePropertyName('updated_at');
                        wrtJSON.WriteValue(DateTimeToStr(Now));
                        wrtJSON.WritePropertyName('image_id');
                        wrtJSON.WriteValue('null');
                        wrtJSON.WritePropertyName('weight_unit');
                        wrtJSON.WriteValue('kg');
                        wrtJSON.WritePropertyName('inventory_item_id');
                        wrtJSON.WriteValue(41738310058150);
                        wrtJSON.WritePropertyName('inventory_quantity');
                        wrtJSON.WriteValue(7);
                        wrtJSON.WritePropertyName('old_inventory_quantity');
                        wrtJSON.WriteValue(7);
                        wrtJSON.WritePropertyName('requires_shipping');
                        wrtJSON.WriteValue(true);
                        wrtJSON.WritePropertyName('admin_graphql_api_id');
                        wrtJSON.WriteValue('gid://39644128051366');
                     wrtJSON.WriteEndObject;
                  wrtJSON.WriteEndArray;
                  wrtJSON.WritePropertyName('options');
                  wrtJSON.WriteStartArray;
                     wrtJSON.WriteStartObject;
                        wrtJSON.WritePropertyName('id');
                        wrtJSON.WriteValue(8479576260774);
                        wrtJSON.WritePropertyName('product_id');
                        wrtJSON.WriteValue(6595121217702);
                        wrtJSON.WritePropertyName('name');
                        wrtJSON.WriteValue('Title');
                        wrtJSON.WritePropertyName('position');
                        wrtJSON.WriteValue(1);
                        wrtJSON.WritePropertyName('values');
                        wrtJSON.WriteStartArray;
                           wrtJSON.WriteValue('Default Title');
                        wrtJSON.WriteEndArray;
                     wrtJSON.WriteEndObject;
                  wrtJSON.WriteEndArray;
               wrtJSON.WriteEndObject;
           wrtJSON.WriteEndArray;
   wrtJSON.WriteEndObject;
   Memo1.Text := wrtJSON.JSON.Format;
 finally
   wrtJSON.DisposeOf;
 end;
end;

Ekran görüntüsü


Gayet kolay ve açıklayıcı olduğunu düşünüyorum. Sizce?

Ustam kusura bakma anca fırsatım oldu. çok çok teşekkür ederim, çok işime yaradı...

(03-05-2021, Saat: 23:32)yanniosman Adlı Kullanıcıdan Alıntı: İşte olay budur :-) Bende Delphi'de çok yeniyim, güzel kaynak oldu. Ellerinize sağlık, çok teşekkür ederim.

ustam size de teşekkür ederim ilginiz için.
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  Fastreport İçindekiler Sayfası Oluşturma [ÇÖZÜLDÜ] bydelphi 1 181 18-03-2024, Saat: 12:11
Son Yorum: bydelphi
  server programinda hani veritabani dogru olur. sadikacar60 4 306 30-01-2024, Saat: 21:06
Son Yorum: sadikacar60
  Uygulamaya responsive görünüm nasıl kazandırabilriz ? delphicim 0 202 04-01-2024, Saat: 15:24
Son Yorum: delphicim
  Delphi 7 ye göre MB Döviz kurları nasıl alırız maydin60 7 964 31-12-2023, Saat: 02:18
Son Yorum: maydin60
  JSON DataSet Serialize for Unidac Halil Han BADEM 2 467 08-11-2023, Saat: 16:53
Son Yorum: Halil Han BADEM



Konuyu Okuyanlar: 1 Ziyaretçi