Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 11 total
Thread TEWBJSONReader how to use
Mon, Mar 5 2018 9:13 PMPermanent Link

KimHJ

Comca Systems, Inc

I have been using the TEWBJSONWriter but never the TEWBJSONReader and I'm a little confused how to use it. It looks like a lot of code to get the value of name.

I have not been able to find any examples.

var
myJson: TEWBJSONReader;
MyResult, MyStr: String;
begin
      myJson := TEWBJSONReader.Create();
      myJson.Initialize(MyResult);
      myjson.BeginObject;
      MyStr :=myjson.GetPropertyName; Here I get the "d"
      if MyStr='d' then
          begin
               myjson.SkipPropertyName;
               myjson.SkipPropertySeparator;         
               myjson.BeginObject;
               MyStr :=myjson.GetPropertyName;  //Here I get the "result"
               if MyStr='result' then
                  begin
                          myjson.SkipPropertyName;
                          myjson.SkipPropertySeparator;
                 end;
             myjson.BeginArray;
             if myjson.IsObject then  // Here I check to see if result is Empty
                begin
                        myjson.BeginObject;
                        MyStr := myjson.GetPropertyName; //Here I get the "_metadata"
               end;
      end;


Now I'm stuck. How do I skip to the name?

I need to see if the result is empty if it's not I need to read the name value.

{
   "d": {
       "results": [
           {
               "__metadata": {
                   "key_fields": "",
                   "rows_affected": -1,
                   "last_autoinc": 0
               },
               "name": "KIMBELL, F"
           }
       ]
   }
}

This how the json looks liker if there is no result.
{
   "d": {
       "results": []
   }
}

Thanks,
Kim
Tue, Mar 6 2018 5:18 AMPermanent Link

Uli Becker

Kim,

> I have been using the TEWBJSONWriter but never the TEWBJSONReader and I'm a little confused how to use it. It looks like a lot of code to get the value of name.

You can use a persistent class and load your json, but parsing arrays is
not that easy as well.

In such a simple case like you described I'd prefer a solution with an
external script, e.g.:

------------------------------------------------------------------

function GetName(myJSON){

var jsonData = JSON.parse(myJSON);
for (var i = 0; i < jsonData.d.results.length; i++) {
    var record = jsonData.d.results[i];
}
   if( jsonData.d.results.length == 0 ) {
      return '';
   } else {
      return record.name;
   }
}

-------------------------------------------------------------------

Add the js file to your project, declare the function:

external function GetName(FJson: String) : String;

and use it like this:

ShowMessage(GetName(MyJSON));

If the length of the array > 1 then you'd have to modify the script -
everything untested.

Uli
Tue, Mar 6 2018 11:28 AMPermanent Link

erickengelke

Avatar

Uli Becker wrote:

Kim,

> I have been using the TEWBJSONWriter but never the TEWBJSONReader and I'm a little confused how to use it. > It looks like a lot of code to get the value of name.

There is a reader/writer chapter in my book on the subject, I use the book a lot because I forget how to do these things.  It's not a lot of code, but there were some tricks, IIRC.

Erick
http://www.erickengelke.com
Tue, Mar 6 2018 12:13 PMPermanent Link

Mark Brooks

Slikware

Avatar

You can just create a persistent class and ignore the array property since you only want the name property (I believe). This will result in hardly any code whatsoever. Easily the quickest and simplest way.
Tue, Mar 6 2018 3:38 PMPermanent Link

Uli Becker

Kim,

<<
There is a reader/writer chapter in my book on the subject, I use the book a lot because I forget how to do these things.  It's not a lot of code, but there were some tricks, IIRC.
>i>

Kim, please buy his book!!  Obviously he needs the money.

Uli
Wed, Mar 7 2018 3:18 PMPermanent Link

KimHJ

Comca Systems, Inc

Uli Becker wrote:
>>Kim, please buy his book!!  Obviously he needs the money. <<

Thanks, I may have forgot tell that I'm creating a module in Delphi. Maybe I should just use the json in Dephi XE 6.

Erik wrote:
>>There is a reader/writer chapter in my book on the subject, I use the book a lot because I forget how to do these >>things.  It's not a lot of code, but there were some tricks, IIRC.<<

I got the book and in the index under json it says page 138, 166 but neither of those pages have anything about json.


Kim
Wed, Mar 7 2018 3:18 PMPermanent Link

KimHJ

Comca Systems, Inc

Uli Becker wrote:
>>Kim, please buy his book!!  Obviously he needs the money. <<

Thanks, I may have forgot tell that I'm creating a module in Delphi. Maybe I should just use the json in Dephi XE 6.

Erik wrote:
>>There is a reader/writer chapter in my book on the subject, I use the book a lot because I forget how to do these >>things.  It's not a lot of code, but there were some tricks, IIRC.<<

I got the book and in the index under json it says page 138, 166 but neither of those pages have anything about json.


Kim
Wed, Mar 7 2018 10:28 PMPermanent Link

Raul

Team Elevate Team Elevate

On 3/5/2018 9:13 PM, KimHJ wrote:
> I have been using the TEWBJSONWriter but never the TEWBJSONReader and I'm a little confused how to use it. It looks like a lot of code to get the value of name.

Like others have said TReader is meant as kind of a parser for properties.

If you do want to go TReader route then something like this should work
(it's a quick hack and likely can be improved).

You did not specify exactly what your JSON contains so this works with
the 2 samples you have and can be extended. however if you get more than
1 array element or other fields this might need to be adjusted a bit
with additional conditionals.

Ideas is to iterate thru the json and then use the fields you need - i'm
just using the "name" but if you need to look at _metadata fields then
you can do so in "r.IsString"

///////////////////////////////////
function ParseJSONForName(const InJSON:string):string;
var
   r:TReader;
   s:String;
begin
   result := '';
   r := TReader.Create;

   try
     r.Initialize(InJSON);
     repeat
        if r.IsObject then
           r.beginobject
        else if r.EndOfObject then
           r.EndObject
        else if r.IsArray then
           r.beginarray
        else if r.EndOFArray then
           r.EndArray
        else if r.IsString then
        begin
         s := r.ReadString;
         if s='rows_affected' or s='last_autoinc' then
         begin
           r.SkipProperty;
           r.SkipPropertySeparator;
           r.SkipPropertyValue;
         end
         else
            r.SkipPropertyName;
        end
        else if r.MoreProperties then
        begin

         s := r.ReadString;
         if s='name' then
         begin
           r.SkipPropertySeparator;
           result := r.ReadString;
         end
         else
         begin
            r.SkipProperty;
            r.SkipPropertySeparator;
            r.SkipPropertyValue;
         end;
        end;
     until (r.level=-1);
   except
     On E:Exception do
      begin
        result := '';
      end;
   end;
   r.Free;
end;

Raul
Thu, Mar 8 2018 11:26 AMPermanent Link

KimHJ

Comca Systems, Inc

Raul wrote:

>>You did not specify exactly what your JSON contains so this works with
the 2 samples you have and can be extended. however if you get more than
1 array element or other fields this might need to be adjusted a bit
with additional conditionals.<<

Thanks Raul.
The 2 samples will be the only thing I get I request a name for an acoount number so there will be no result or a name.

Kim
Fri, Mar 9 2018 2:10 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Kim,

<< Now I'm stuck. How do I skip to the name? >>

Use this:

...          if (not myjson.EndOfArray) then
               begin
               myjson.BeginArray;
               while True do
                  begin
                  if myjson.IsObject then  // Here I check to see if result is Empty
                      begin
                        myjson.BeginObject;
                        MyStr := myjson.GetPropertyName; //Here I get the "_metadata"
                      end;
                  if (not myjson.MoreArrayElements) then
                      Break;
                  end;
               myjson.EndArray;
               end;  
...

Tim Young
Elevate Software
www.elevatesoft.com
Page 1 of 2Next Page »
Jump to Page:  1 2
Image