我正在尝试使用Coinbase的API,并希望将它们的价格用作浮点数,但该对象会返回一个API对象而我不知道如何转换它。
例如,如果我致电<?php
/**
* This is the model class for table "business".
*
* The followings are the available columns in table 'business':
* @property integer $id
* @property string $business_name
* @property string $image
* @property string $business_description
* @property string $opening_hours
* @property string $closing_hours
* @property string $days
* @property string $Holiday
* @property string $website
* @property integer $phone
*
* The followings are the available model relations:
* @property Address[] $addresses
* @property BusinessItems[] $businessItems
* @property BusinessPackage[] $businessPackages
* * @property BusinessPhotos[] $businessPhotoses
* @property ClaimBusiness[] $claimBusinesses
* @property Facilities[] $facilities
* @property ReviewBusiness[] $reviewBusinesses
* @property SubCategoryBusiness[] $subCategoryBusinesses
*/
class Business extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'business';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('business_name, business_description, opening_hours, closing_hours, days', 'required'),
array('phone', 'numerical', 'integerOnly'=>true),
array('business_name', 'length', 'max'=>60),
array('image, opening_hours, closing_hours, days, Holiday, website', 'length', 'max'=>45),
array('business_description', 'length', 'max'=>500),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, business_name, image, business_description, opening_hours, closing_hours, days, Holiday, website, phone', 'safe', 'on'=>'search'),
array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'safe' => false,'on'=>'insert,update'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
'businessItems' => array(self::HAS_MANY, 'BusinessItems', 'business_id'),
'businessPackages' => array(self::HAS_MANY, 'BusinessPackage', 'business_id'),
'businessPhotoses' => array(self::HAS_MANY, 'BusinessPhotos', 'business_id'),
'claimBusinesses' => array(self::HAS_MANY, 'ClaimBusiness', 'business_id'),
'facilities' => array(self::HAS_MANY, 'Facilities', 'business_id'),
'reviewBusinesses' => array(self::HAS_MANY, 'ReviewBusiness', 'business_id'),
'subCategoryBusinesses' => array(self::HAS_MANY, 'SubCategoryBusiness', 'business_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'business_name' => 'Business Name',
'image' => 'Image',
'business_description' => 'Business Description',
'opening_hours' => 'Opening Hours',
'closing_hours' => 'Closing Hours',
'days' => 'Days',
'Holiday' => 'Holiday',
'website' => 'Website',
'phone' => 'Phone',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('business_name',$this->business_name,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('business_description',$this->business_description,true);
$criteria->compare('opening_hours',$this->opening_hours,true);
$criteria->compare('closing_hours',$this->closing_hours,true);
$criteria->compare('days',$this->days,true);
$criteria->compare('Holiday',$this->Holiday,true);
$criteria->compare('website',$this->website,true);
$criteria->compare('phone',$this->phone);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Business the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
,它将返回此信息:
client.get_spot_price()
我只想要{
"amount": "316.08",
"currency": "USD"
}
。我该如何解决?
答案 0 :(得分:1)
data = {
"amount": "316.08",
"currency": "USD"
}
price = float(text['amount'])
使用API使用JSON解析器
import json
data = client.get_spot_price()
price = float(json.loads(data)['amount'])
print price
答案 1 :(得分:0)
它看起来像json
输出。您可以在python中导入json
库并使用loads
方法读取它,以获取示例:
import json
# get data from the API's method
response = client.get_spot_price()
# parse the content of the response using json format
data = json.loads(response )
# get the amount and convert to float
amount = float(data['amount'])
print(amount)
答案 2 :(得分:0)
首先,将返回的对象放入变量并检查返回值的类型。 就像这样:
打印类型(your_returned object / variable)
如果这是字典您可以通过字典键从字典访问数据。字典结构是:
dict = {key_1:value_1,key_2:value_2,...... key_n:value_n}
1.您可以访问字典的所有值。 如下所示:
print dict [key_1] #output将返回value_1
INT(your_data) 转换为浮动: 浮动(your_data)
如果它不是字典,您需要通过以下方式将其转换为字典或json:
json.loads(你的返回对象)
在您的情况下,您可以这样做:
variable = client.get_spot_price()
print type(variable) #if it is dictionary or not
print float(variable["amount"]) #will return your price in float