Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 03-12-2017, 07:42 PM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 209
Default How Do I parse a JSON string with just one value

I have the following JSON string and need to get the value of 'token' but all searched bright up JSON arrays etc which may be overkill for extracting one value, any help would be appreciated.

Code:
{
  "token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODk1MTUxMjMsImlkIjoiVWx0aW1hdGUgRmlsZSBNYW5hZ2VyIFRORyIsIm9yaWdfaWF0IjoxNDg5NDI4NzIzLCJ1c2VyaWQiOjQ2NTgzMSwidXNlcm5hbWUiOiJaYWFrYTUxOTcyIn0.r7cjC_rQapv8lZhLeseaxoTXsueCVAwNhwU_ytAFK50i9lMMMGwRyiRacFGF6d5q35oZ3sfaA97B1gLWEgfWEblSW6UmIPDETR07h5KzsNo6xxS74HonN-3jsnibOy61vJO8XmUiQXT2zI-v1JtvOmxgIrEQVGdt2PXNFIgR7XVXmJV54fODuXPRBmrCvbTe5j-ewvPHYfJxxQ4GccXwJ4W2R0FaMPKAXNB56IFE_BtJE_RNU9H8vSdlDzlKKzOz76KD8o0bBTCu_kYfoE5FTncjqs-w-i40SAFFcqlLVQms4P6h-KcNb5CQrv9vfo211jr1bctc50rYQEmAFV4JoQ"
}
Running Delphi 10.1 Update 2 (Anniversary edition)

Last edited by Zaaka; 03-13-2017 at 07:44 PM.
  #2  
Old 03-13-2017, 11:11 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,201
Default

If you don't want a complete JSON implementation I would do some simple string-slicing

In pseudo:
Locate "token": in x
Find the position of the next " in x1
From this position find the next " in x2
Copy the value between x1 and x2
  #3  
Old 03-13-2017, 04:49 PM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 209
Default

I have done it as you suggested, but just to get my head around the new JSON system, how would I do it with TJSon etc, by the way the token value is a unicode string as it can be larger that 255 chars (see original post, updated to include full string)

Last edited by Zaaka; 03-13-2017 at 07:45 PM.
  #4  
Old 03-14-2017, 08:45 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,201
Default

JSON example: Stackoverflow
But I'm not sure if D2010 has JSON already, I thought it did, but no guarantees...

As for the 255 limit, there is none, in your code you copy max 255, but you just Copy x2 - x1 characters. If that's more then 255 there's still no problem. Then the problem is in your code, but then you'll have to post your code...
  #5  
Old 03-14-2017, 10:09 PM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 209
Default

Its OK now I have sorted it, for anybody who is curious heres the code

sResponse is the Response from the idHTTP Get which is my original JSON string in original question.

Code:
var
  jsonObjectLevel1: TJSONObject;
BEGIN
jsonObjectLevel1 := TJSONObject.ParseJSONValue
    (TEncoding.UTF8.GetBytes(sResponse), 0) as TJSONObject;
  try
    frmMainForm.memTVDBAuthToken.Text := jsonObjectLevel1.GetValue
      ('token').Value;
    frmMainForm.memTVDBAuthToken.Lines.SaveToFile
      (cf(ExtractFilePath(ParamStr(0))) + 'TVDBToken.text');
  finally
    jsonObjectLevel1.Free;
  end;
Closed Thread

Tags
delphi, json, unnested

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:33 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.