Recent

Author Topic: String  (Read 1448 times)

michoux

  • Full Member
  • ***
  • Posts: 112
String
« on: January 30, 2023, 06:10:22 am »
Hello,

I am trying to parse a big JSON string.

Code: Pascal  [Select][+][-]
  1. data := TJSONArray(GetJSON(rawJson));

above gives me an error.
"string exceeds end of line 1"

I do not know what above error points to.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: String
« Reply #1 on: January 30, 2023, 08:54:44 am »
This error means usually that the underlying rawJson string is not properly closed.
In the case of the jsonparser it may be that the json is malformed, although I expect an exception in that case.
Another remote possibility is that the code is compiled with string = shortstring, so make sure the compiler is in {$H+} state.
« Last Edit: January 30, 2023, 09:00:53 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #2 on: February 05, 2023, 09:22:39 am »
Hi,

Json looks ok.
I have  {$mode objfpc}{$H+}

It is possible that string is just to big for parser?
inside Json I have a base64 decoded picture, which is really long one ...

If I remove some part of base64 string then it is good.

Regards Mitch
« Last Edit: February 05, 2023, 09:24:31 am by michoux »

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: String
« Reply #3 on: February 05, 2023, 09:32:50 am »
You can parse your json data at https://jsoneditoronline.org/
If there is no problem during this process, it will probably be fine on Lazarus as well.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #4 on: February 05, 2023, 10:53:02 am »
It parses ok in web page.

LIke I said if I remove let say 1/3 of the image string inside the json data, then it works also in lazarus.
It really looks like the string is to big for the parser.

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: String
« Reply #5 on: February 05, 2023, 10:57:07 am »
If I remember correctly, I parsed json strings of about ~50 - 80MB without any problems. I think it might be a character issue, not a dimensional one.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: String
« Reply #6 on: February 05, 2023, 10:59:39 am »
Without Json to test. It is a string close.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #7 on: February 05, 2023, 11:04:32 am »
It is possible yes that there is some strange char inside the picture attribute, but it is really long one and hard to find it.
Any way to debug where all crashes?


michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #8 on: February 05, 2023, 11:19:34 am »
I am attaching the json.


paweld

  • Hero Member
  • *****
  • Posts: 996
Re: String
« Reply #9 on: February 05, 2023, 12:06:08 pm »
json is correct and fpjson don't show any errors
Code: Pascal  [Select][+][-]
  1. uses
  2.   fpjson, LazUTF8;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   jd: TJSONData;
  7.   sl: TSTringList;
  8.   i: Integer;
  9.  
  10.   procedure GetObject(jo: TJSONObject);
  11.   var
  12.     j: Integer;
  13.     s: String;
  14.   begin
  15.     for j := 0 to jo.Count - 1 do
  16.     begin
  17.       if jo.Items[j].IsNull then
  18.         s := 'NULL'
  19.       else
  20.       begin
  21.         s := jo.Items[j].AsString;
  22.         if UTF8Length(s) > 50 then
  23.           s := UTF8Copy(s, 1, 50) + '  ...';
  24.       end;
  25.       Memo1.Lines.Add(Format('Name: %s Value: %s', [jo.Names[j], s]));
  26.     end;
  27.     Memo1.Lines.Add('---------');
  28.   end;
  29.  
  30. begin
  31.   sl := TStringList.Create;
  32.   sl.LoadFromFile('test.txt');
  33.   jd := GetJSON(sl.Text);
  34.   if jd.JSONType = jtArray then
  35.   begin
  36.     for i := 0 to TJSONArray(jd).Count - 1 do
  37.       GetObject(TJSONArray(jd).Objects[i]);
  38.   end
  39.   else
  40.     GetObject(TJSONObject(jd));
  41.   jd.Free;
  42.   sl.Free;
  43. end;
Best regards / Pozdrawiam
paweld

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: String
« Reply #10 on: February 05, 2023, 12:16:22 pm »
It's ok to read the json, probably something wrong with the way you read the txt file.
The working example and its view are attached.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #11 on: February 06, 2023, 09:35:22 am »
Hi

I managed to get things working and parse the json.
Thank you very much.

Now I have a new issue.
I read the picture with following command.
 slika:= person.FindPath('figure').AsString;

This is original part of data in Json
data:image\/jpeg;base64,\/9j\/4AA .........."

But when I read the figure to string I get  following

data:image/jpeg;base64,/9j/4AA

So missing all the "\" chars

michoux

  • Full Member
  • ***
  • Posts: 112
Re: String
« Reply #12 on: February 06, 2023, 10:06:23 am »
Solved by replacing \ with \\ in json.


dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: String
« Reply #13 on: February 06, 2023, 11:34:00 am »
Hi

I managed to get things working and parse the json.
Thank you very much.

Now I have a new issue.
I read the picture with following command.
 slika:= person.FindPath('figure').AsString;

This is original part of data in Json
data:image\/jpeg;base64,\/9j\/4AA .........."

But when I read the figure to string I get  following

data:image/jpeg;base64,/9j/4AA

So missing all the "\" chars

loaded showed in post #10 how to read picture from base64 (you don't have to replace \ with \\).

 

TinyPortal © 2005-2018