2017-07-26 79 views
0

我想解析一個json文件並從中提取一個鍵的值,並將它傳遞給另一個post請求。不過,我不能夠提取他們的價值,當我嘗試,它打印的密鑰本身,而不是價值從json打印一個鍵值

PFB JSON文件

{ 
    "data":{ 
     "isActivated":true, 
     "userDetails":{ 
     "userId":"52321713-add8-4455-9e0c-426eab923338", 
     "oktaId":"00ub24c5bs6awQyBD0h7", 
     "contactId":"7234294092390", 
     "oktaSessionToken":"20111UqAZ9-E1YPlNcXBLRCu_ZHHzBCH2q_j01yiIkPyRp5-0E7HAQQ", 
     "oktaSessionId":"102a9q79TrqRWek9vHEPkP3yQ", 
     "apiToken":"f5c95fd8-efc4-497e-8128-51a014de3a9a", 
     "firstName":"Judy", 
     "lastName":"Test1", 
     "middleName":null, 
     "email":"[email protected]", 
     "isEmployee":true, 
     "pushNotificationStatus":true 
     }, 
     "companyDetails":{ 
     "profileScreenBackgroundColor":"13253D", 
     "companyColor":"7ED321", 
     "companyName":"Mobile App Demo", 
     "companyLogo":"http://", 
     "isSSO":false 
     } 
    } 
} 

PFB哈希文件:

  {"data"=>{"isActivated"=>true, "userDetails"=> 
      {"userId"=>"52321713-add8-4455-9e0c-426eab923338", 
      "oktaId"=>"00ub24c5bs6awQyBD0h7", "contactId"=>"0033300001tZ8k5AAC", 
     "oktaSessionToken"=>"201112Ncbw364pHojkD4UlzGb1knz9UTZPIy2LFDn9Tgy_FmgEpZmmU", 
    "oktaSessionId"=>"102Kd-c2yEeSnmwr3YKX8qeyg", 
"apiToken"=>"f8f070e2-e51b-4d69-8b1a-b7b63d25e781", 
"firstName"=>"Judy", "lastName"=>"Test1", 
"middleName"=>nil, 
"email"=>"[email protected]", 
"isEmployee"=>true, 
"pushNotificationStatus"=>true}, 
"companyDetails"=>{"profileScreenBackgroundColor"=>"13253D", "companyColor"=>"7ED321", 
"companyName"=>"Mobile App Demo", "companyLogo"=>"https:", 
"isSSO"=>false}}} 

下面的代碼:

我曾試過幾乎所有的手段,不知道我錯過了什麼。

apitoken = RestClient.post("https://", {'email'=>arg,'password'=>'abcs','deviceUUId'=>'udid', 'deviceTypeId'=>1}.to_json, { "Content-Type" => 'application/json','buildNumber' => '000','deviceTypeId'=>'9'}) 
    puts apitoken 
    puts "**************" 
    puts apitoken["apiToken"] 
    logindetails = JSON.parse(apitoken) 
    tada = JSON.parse(logindetails)['data']['apitoken'] 
    puts tada 
    puts logindetails 
    result = logindetails["data"]["apiToken"] 
    puts result 
    puts "**************" 
    logindetails.each do |logindetail| 
    puts logindetail 
    puts logindetail["apiToken] 
    puts "**************" 
    end 
    result = logindetails['apiToken'] 
    puts result 
    end 

我得到的輸出是apiToken而不是它的值。任何幫助是極大的讚賞。

+0

一行程序不可讀。格式化它們以提高可讀性 –

+1

應該是'logindetails ['data'] ['userDetails'] ['apiToken']' –

+0

@SergioTulentsev編輯 –

回答

2

令牌是下datauserDetailsapiToken:該長度的

json['data']['userDetails']['apiToken'] #=> f5c95fd8-efc4-497e-8128-51a014de3a9a