1 (edited by prahousefamily 2022-08-23 12:47:26)

Topic: How To Show Index,Name,Value Json To Memo

if have json string

{
    "result": {
        "app_name": "Test",
        "app_date": "2022-01-01",
        "app_time": "12:30:40"
    }
}

and to result to Form1.memo1.Lines.Add();

result size = 3
index = 0, name = app_name, value = test
index = 1, name = app_date, value = 2022-01-01
index = 2, name = app_time, value = 12:30:40

How to Edit Script

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Show Index,Name,Value Json To Memo

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
sl:TStringList;
jDataStream: TJSONObject;
jValue_01, jValue_02, jValue_03, jValue_04: TJSONString;
jArray: TJSONArray;
i: int;
begin
      sl:=TStringList.Create;
      sl.LoadFromFile(ExtractFilePath(Application.ExeName)+'\json.txt');
      jDataStream := nil;

      jDataStream:=TJSONObject(TJSONObject.ParseJSONValue(sl.Text));
      frm_main.Memo1.Text:=sl.text;
      frm_main.Memo2.Text:=''; //clear

      if jDataStream <> nil then
       begin
            jValue_01 := TJSONString(jDataStream.GetPairByIndex(0).JsonValue);
            jValue_02 := TJSONString(jDataStream.GetPairByIndex(4).JsonValue);
            jValue_03 := TJSONString(jDataStream.GetPairByIndex(2).JsonValue);
            jValue_04 := TJSONString(jDataStream.GetPairByIndex(3).JsonValue);
            frm_main.Memo2.Lines.Add(jValue_01.Value);
            frm_main.Memo2.Lines.Add(jValue_02.Value);
            frm_main.Memo2.Lines.Add(jValue_03.Value);
            frm_main.Memo2.Lines.Add(jValue_04.Value);

     end;

      //free object
      sl.free
end;

3 (edited by prahousefamily 2022-08-23 12:47:14)

Re: How To Show Index,Name,Value Json To Memo

Thank You StateOne For Code But It Failed

https://i.postimg.cc/RCdf8K0j/Image-1.png

----------

After I try Fix Code but incomplete, missing some value
I want results like this

result size = 3
index = 0, name = app_name, value = test
index = 1, name = app_date, value = 2022-01-01
index = 2, name = app_time, value = 12:30:40

How To Fix IT Show  Name  Or Add Class Component ??? Help Me Please !

https://i.postimg.cc/GtgQGSS1/2022-08-23-18-31-19.png

Code How To Fix ???

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
sl:TStringList;
jDataStream: TJSONObject;
jObject: TJSONObject;
i : int;
appindex,appname,appvalue:String;
begin
      sl:=TStringList.Create;
      sl.LoadFromFile(ExtractFilePath(Application.ExeName)+'\json.txt');
      jDataStream := nil;
      jDataStream := TJSONObject(TJSONObject.ParseJSONValue(sl.Text));
      jObject := TJSONObject(jDataStream.GetPairByName('result').JsonValue);
      Form1.Memo1.Text:=sl.text;
      Form1.Memo2.Text:=''; //clear
      if jDataStream <> nil then
       Form1.Memo2.Lines.Add('result size ='+ IntToStr(jObject.Size)) ;
       begin
           For i := 0 To jObject.Size -1  Do
           Begin
           appindex := 'index = '+IntToStr(i);
           appname  := 'name = '  ;
           appvalue := 'value = '+ TJSONString(jObject.GetPairByIndex(i).JsonValue).value ;
           Form1.Memo2.Lines.Add(appindex+', '+appname+', '+appvalue) ;
           End;
       end;
      sl.free
end ;
begin
end.
My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Show Index,Name,Value Json To Memo

const raw_json =
 '{ ' +
  '    "result": { ' +
  '        "app_name": "Test", ' +
  '        "app_date": "2022-01-01", ' +
  '        "app_time": "12:30:40" ' +
  '    } ' +
  '} ';

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var sc,j : variant;
begin
 sc:=CreateOleObject('ScriptControl');
 sc.Language := 'JavaScript';
 j := sc.Eval('('+raw_json+')');
 
 ShowMessage(j.result.app_name);
 ShowMessage(j.result.app_date);
 ShowMessage(j.result.app_time);
 
 sc := 0;
end;

Re: How To Show Index,Name,Value Json To Memo

Thank You vovka3003 For Code Example Use 'javascrip' Method

But not

.....

i will input integer [0]  result  String  ✔️"app_name" Not  ❌ "Test"
i will input integer [1]  result  String  ✔️"app_date"   Not  ❌ "2022-01-01"
i will input integer [2]  result  String  ✔️"app_date"   Not  ❌ "12:30:40t"

OR 

i will input string  ['app_name']  result  Integer  ✔️ 0 
i will input string  ['app_date']    result  Integer  ✔️1 
i will input string  ['app_time']    result  Integer  ✔️2

How To use Class Or Method result ???

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Show Index,Name,Value Json To Memo

prahousefamily wrote:

Thank You vovka3003 For Code Example Use 'javascrip' Method

But not

.....

i will input integer [0]  result  String  ✔️"app_name" Not  ❌ "Test"
i will input integer [1]  result  String  ✔️"app_date"   Not  ❌ "2022-01-01"
i will input integer [2]  result  String  ✔️"app_date"   Not  ❌ "12:30:40t"

OR 

i will input string  ['app_name']  result  Integer  ✔️ 0 
i will input string  ['app_date']    result  Integer  ✔️1 
i will input string  ['app_time']    result  Integer  ✔️2

How To use Class Or Method result ???

Ничего не понял, но очень интересно...

Re: How To Show Index,Name,Value Json To Memo

http://digital-flame.ru/2018/06/28/delp … znacheniy/

8 (edited by prahousefamily 2022-08-25 04:49:33)

Re: How To Show Index,Name,Value Json To Memo

Thank You Very Much pavlenko.vladimir.v For Link
I try script but error not have class or component in MVD
========================================

vovka3003 give script It Good but   result ➝ JsonString
example
"app_name": "Test"
Scritp By vovka3003 result = "Test"


I don't know what it's called. Name Or KEY OR Field Name ..... But I will input number index ➝ result ➝ xxx
example
"app_name": "Test"
input number 0
result = "app_name"
or Reverse  I will input String Field Name For Find Index ➝ result ➝ index number
example
"app_name": "Test"
input String  "app_name"
result = 0


Help me Please vovka3003 or pavlenko.vladimir.v ! Everybody
Thank You Again

https://i.postimg.cc/HLm9gMmQ/D20220825-T114652.png

My Visual Database : I Love You
Easy For Beginner Student For Me

9 (edited by pavlenko.vladimir.v 2022-08-25 10:12:15)

Re: How To Show Index,Name,Value Json To Memo

Я не смог получить ключ пары
I couldn't get the pair key
hmm

10 (edited by vovka3003 2022-08-25 20:53:29)

Re: How To Show Index,Name,Value Json To Memo

И тут наблюдается XYZ-problem...
Какая глобальная задача вообще..?

11 (edited by sparrow 2022-09-18 20:19:54)

Re: How To Show Index,Name,Value Json To Memo

Hi all


var
  sl:TStringList;
  jDataStream: TJSONObject;
  jObject: TJSONObject;
  i : int;
  appindex,appname,appvalue:String;
  RegExp: TRegExp;
begin
      sl:=TStringList.Create;
      sl.LoadFromFile(ExtractFilePath(Application.ExeName)+'\json.txt');
      jDataStream := nil;
      jDataStream := TJSONObject(TJSONObject.ParseJSONValue(sl.Text));
      jObject := TJSONObject(jDataStream.GetPairByName('result').JsonValue);
      Form1.Memo1.Text:=sl.text;
      Form1.Memo2.Text:=''; //clear
      if jDataStream <> nil then
      begin
       Form1.Memo2.Lines.Add('result size ='+ IntToStr(jObject.Size)) ;
       RegExp := TRegExp.Create('"\w+":');
       RegExp.InputString := TJSONValue(jDataStream.GetPairByName('result').JsonValue).ToString;
       RegExp.Exec;
           For i := 0 To jObject.Size -1  Do
           Begin
             appindex := 'index = ' + IntToStr(i);
             appname  := 'name = ' + Copy(RegExp.Match[0], 2, Length(RegExp.Match[0]) - 3) ;
             appvalue := 'value = ' + TJSONString(jObject.GetPairByIndex(i).JsonValue).value ;
             Form1.Memo2.Lines.Add(appindex+', '+appname+', '+appvalue) ;
             RegExp.ExecNext
           End;
       end;
      sl.free;
      RegExp.Free;
end ;

Re: How To Show Index,Name,Value Json To Memo

Thank sparrow For Best Example Good Code By Use Tregexp

https://i.ibb.co/cLFFJ4y/D20220929-T065738.png

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To Show Index,Name,Value Json To Memo

\w+

\w - Any word character
+ - One or more