从json

时间:2017-07-26 09:09:00

标签: json ruby

我正在尝试解析json文件并从中提取密钥的值,并将其传递给另一个post请求。但是,我无法提取它们的值,当我尝试时,它会打印键本身而不是值

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":"abc@mailinator.com",
         "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"=>"judy.test1@mailinator.com",
 "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而不是它的值。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

令牌位于data userDetails apiToken

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