0

I have these JSON Object , I read it well , But when i read oauth value i got an empty result ,

How I can read it ?

{
        "status": true,
        "message": "Success",
        "items": {
            "id": 6,
            "name": "Tesst1",
            "email": "[email protected]",
            "image": "https://www.website.com/images/man.png",
            "created_at": "2017-08-24 09:18:15",
            "updated_at": "2017-08-24 09:18:15",
            "oauth": {
                "token_type": "abc",
                "expires_in": 1296000,
                "access_token": "jI5NiwiZXhGVzIjpbXX0.RaC3ixxUeyYnCB6vNlwKsdjf09UeOwUJlcKmKErmE_LTAeQ-4fm8iBOKqUgpikTkyB3ztDGf4DAsaeEjUMqH76jZdbPHnX0vr676dCXkEunWoDEB8sYiHz7XRVgQ5W0O9yybA93mPO_XyrWPibkGW7GLQOApRD605N0e6vw0v9Kb_WQBim7zjTNqoLM1fSjgKJezFqf9_s3KIqBc4bjsayYLl7duzo2fzRmWtnGFfbsgO6YcaIz8ezNtWbtixLRMKnJEj1-MluqjWubsbq_gTI6yiyyac3_oY22Ge0QDdCtljadgO7wRz5VT5aJkxmJRB90u0ovm0tpGzYOO_4giY-J5egpOptjt2VZbPeM-vWPEo4-c6NYMZx6WqxBHjkZwiKUsM-tufzl5P5lFRJ9V_rBUBHEiSonTAk9FVDAwjqLc6N_IC1lsFDEC_3NBy4",
                "refresh_token": "def502003e7826477c1072497ad66d7f11ea29e81a0fafef6223a63b6a0a3d18de71165afb9a340d10facca3ac7ee955aac5786a5c66a39cdf77e3f6449458e07271cbcde699aabf4d7f72dad10d586c37497216552f88460e50e9ea4944214984d5b23bac04b5f8265d132"
            }
        }
    }

If i read the oauth as a JSON Object I got an exception about unsupported conversion,

conversion from TJSONObject to string is not supported

what is the wrong with my code ??

if JsonRespnse <> '' then
    begin
      jsonObiekt := TJSONObject.ParseJSONValue
        (TEncoding.ASCII.GetBytes(JsonRespnse), 0) as TJSONObject;
      try
        try
          LoginValue := jsonObiekt.get('status').JsonValue;
          LoginValueIsTrue := StrToBool(LoginValue.value);

          if LoginValueIsTrue = True then
          begin
            ItemsValue := jsonObiekt.get('items').JsonValue;
            if ItemsValue is TJSONObject then
            begin
              strIdValue := ItemsValue.GetValue<string>('id');
              // strOuthValue := ItemsValue.GetValue<string>('oauth');
              OauthValue := TJSONObject(ItemsValue).get('oauth').JsonValue;
              if OauthValue is TJSONObject then
                Memo1.Lines.Add('oauth : ' + OauthValue.value);
            end;
          end;
        except
          on E: Exception do
            ShowMessage('Read JSON : ' + E.Message);
        end;
      finally
        jsonObiekt.Free;
      end;
    end;
0

1 Answer 1

2
strOuthValue := ItemsValue.GetValue<string>('oauth');

Is not working because it’s not a string in the context of the TJSON* classes. The key oauth is an json object just like 'items'.

You can read the values of the oauth object like you did it with 'items.id'

 if OauthValue is TJSONObject then
 begin
   Memo1.Lines.Add('oauth : ' + TJSONObject(OauthValue).ToString);
   Memo1.Lines.Add('oauth.token_type: ' + TJSONObject(OauthValue).Values['token_type'].ToString);
 end;

Does this cover your question?

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for reply, but the problem is about how you get the oauth content in OauthValue ?
@Phillipp H. oauth is an object that in item object .
In my answer is an example that shows how you can read the values of the oauth object.
this is the question answer : OauthValue := ItemsValue.GetValue<TJSONValue>('oauth');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.