Delphi Can
String değer okuma - Baskı Önizleme

+- Delphi Can (https://www.delphican.com)
+-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3)
+--- Forum: Genel Programlama (https://www.delphican.com/forumdisplay.php?fid=6)
+--- Konu Başlığı: String değer okuma (/showthread.php?tid=6887)



String değer okuma - akuyumcu63 - 13-08-2022

merhaba;
dünya tarihi için 
http://worldtimeapi.org/api/ip
adresinden aşağıdaki bilgiyi okuyorum.

{"abbreviation":"+03","client_ip":"5.24.30.18","datetime":"2022-08-13T14:33:01.340405+03:00","day_of_week":6,"day_of_year":225,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":10800,"timezone":"Europe/Istanbul","unixtime":1660390381,"utc_datetime":"2022-08-13T11:33:01.340405+00:00","utc_offset":"+03:00","week_number":32}

amacım bu veri içindeki datetime":"2022-08-13 bilgiden 2022-08-13 bu tarih bilgisini okumak. 

AnsiMidStr(deger, 60 , 10)

bu şekilde okuma işlemini yapıyorum. ancak gelen bilgi günlük değişebiliyor. bu durumda tarih bilgisinin konumu değişiyor ve hatlı bilgi okunmuş oluyor.

günün tarih bilgisini doğru okumak için ne yapabilirim.


Cvp: String değer okuma - vedat35 - 13-08-2022

(13-08-2022, Saat: 14:48)akuyumcu63 Adlı Kullanıcıdan Alıntı: merhaba;
dünya tarihi için 
http://worldtimeapi.org/api/ip
adresinden aşağıdaki bilgiyi okuyorum.

{"abbreviation":"+03","client_ip":"5.24.30.18","datetime":"2022-08-13T14:33:01.340405+03:00","day_of_week":6,"day_of_year":225,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":10800,"timezone":"Europe/Istanbul","unixtime":1660390381,"utc_datetime":"2022-08-13T11:33:01.340405+00:00","utc_offset":"+03:00","week_number":32}

amacım bu veri içindeki datetime":"2022-08-13 bilgiden 2022-08-13 bu tarih bilgisini okumak. 

AnsiMidStr(deger, 60 , 10)

bu şekilde okuma işlemini yapıyorum. ancak gelen bilgi günlük değişebiliyor. bu durumda tarih bilgisinin konumu değişiyor ve hatlı bilgi okunmuş oluyor.

günün tarih bilgisini doğru okumak için ne yapabilirim.


Function GetJSONParamByName(JSonData, ReqPrm:String):String;
Var
Line, nSearchString:String;
List:TStringList;
I, nPos:Integer;
 
Begin
Result:='?';
if Length(JSonData)<3 then Exit;
JSonData[Low(JSonData)]:=' ';
JSonData[High(JSONData)]:=' ';
JSonData:=Trim(JSonData);
nSearchString:='"'+ReqPrm+'":';
List:=TStringList.Create;
List.Text:=StringReplace(JSonData, ',', chr(13), [rfReplaceAll, rfIgnoreCase]);
for I:=0 To List.Count-1 do
Begin
 Line:=List.Strings[I];
 nPos:=Pos(nSearchString, Line);
 if (nPos>0) then
 Begin
  Result:=Copy(Line, nPos+nSearchString.Length, Length(Line));
  if Result[Low(Result)] = '"' then
   Result[Low(Result)]:=' ';
  if Result[High(Result)] = '"' then
   Result[High(Result)]:=' ';
  Result:=Trim(Result);
  Break;
 End;  
End;
List.Free;
End;



procedure TForm1.FormCreate(Sender: TObject);
Var
Veri:String;

begin
Veri:='{"abbreviation":"+03","client_ip":"5.24.30.18","datetime":"2022-08-13T14:33:01.340405+03:00","day_of_week":6,'+
      '"day_of_year":225,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":10800,"timezone":"Europe/Istanbul",'+
      '"unixtime":1660390381,"utc_datetime":"2022-08-13T11:33:01.340405+00:00","utc_offset":"+03:00","week_number":32}';

showmessage( GetJSONParamByName(Veri, 'datetime') );

halt;
end;



Cvp: String değer okuma - SimaWB - 13-08-2022

(13-08-2022, Saat: 14:48)akuyumcu63 Adlı Kullanıcıdan Alıntı: merhaba;
dünya tarihi için 
http://worldtimeapi.org/api/ip
adresinden aşağıdaki bilgiyi okuyorum.

{"abbreviation":"+03","client_ip":"5.24.30.18","datetime":"2022-08-13T14:33:01.340405+03:00","day_of_week":6,"day_of_year":225,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":10800,"timezone":"Europe/Istanbul","unixtime":1660390381,"utc_datetime":"2022-08-13T11:33:01.340405+00:00","utc_offset":"+03:00","week_number":32}

amacım bu veri içindeki datetime":"2022-08-13 bilgiden 2022-08-13 bu tarih bilgisini okumak. 

AnsiMidStr(deger, 60 , 10)

bu şekilde okuma işlemini yapıyorum. ancak gelen bilgi günlük değişebiliyor. bu durumda tarih bilgisinin konumu değişiyor ve hatlı bilgi okunmuş oluyor.

günün tarih bilgisini doğru okumak için ne yapabilirim.

Elinizdeki veri JSON olduğu için JSON parse işlemi ile kolayca istediğiniz değeri okuyabilirsiniz. 
Webten okuduğunuz bilgiyi jsonData adında string bir değişkene attığınızı farzedersek:
var
  jsonValue: TJSONValue;
begin
  jsonValue:= TJSONObject.ParseJSONValue(jsonData);
  if jsonValue <> nil then
  begin
    ShowMessage(jsonValue.GetValue<string>('datetime'));
    jsonValue.Free;
  end;
end;



String değer okuma - enigma - 13-08-2022

Eğer daha basit bir şey isterseniz,

gelen veriyi gelenveri adında bir değişkene atadığımız farz edersek,

var p: integer;
gelenveri, tarih: string;

p := pos("datetime:", gelenveri);
if p <> 0 then tarih := copy(gelenveri, p+10, 10);

şeklinde de yapabilirsiniz.


Cvp: String değer okuma - akuyumcu63 - 15-08-2022

merhaba;

değerli bilgiler için çok teşekkür ederim.