2017-08-24 73 views
0

我的这些JSON对象,我读得很好,但是当我读到的OAuth值我有一个空的结果,阅读内JSON对象德尔福

我怎样才能看懂了吗?

{ 
     "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" 
      } 
     } 
    } 

如果我读的OAuth作为JSON对象我得到了不支持的转换异常,从TJSONObject

转换为字符串不支持

什么是错的用我的代码?

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; 

回答

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

不工作,因为它不是在TJSON *类的上下文中的字符串。关键的oauth就像'items'一样是一个json对象。

像你这样做是与“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; 

这是否覆盖你的问题,你可以阅读的OAuth对象的值是多少?

+0

谢谢你的答复,但问题是你如何得到'OauthValue'的OAuth的内容? –

+0

@Phillipp H. oauth是项目对象中的一个对象。 –

+1

在我的回答是一个示例,显示如何读取oauth对象的值。 –