2016-02-24 23 views
0

當我調用REST API象下面這樣:SoftLayer API選項跳過一些返回的屬性?

GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/206/getCategories.json?objectMask=id;name;categoryCode;groups.prices.item;groups.prices.attributes.itemPriceAttributeType;packageConfigurations.isRequired;packageConfigurations.orderStepId;packageConfigurations.sort 

我得到迴應的身體像以下(僅部分列出):

[ 
    { 
     "categoryCode" : "hub", 
     "groups" : [ 
     { 
      "itemCategoryId" : 74, 
      "packageId" : 206, 
      "prices" : [ 
       { 
        "attributes" : [], 
        "currentPriceFlag" : null, 
        "id" : 30920, 
        "item" : { 
        "capacity" : "0", 
        "description" : "Object Storage - Pay as you go", 
        "id" : 4069, 
        "itemCategory" : { 
         "categoryCode" : "hub", 
         "groups" : [ 
          { 
           "itemCategoryId" : 74, 
           "packageId" : 206, 
           "prices" : [ 

我想所有groups.prices.item的信息,但是我不希望groups.prices.item擁有的「itemCategory」數據。是否有一個選項可以排除某些屬性,以便返回的數據不包含該特定數據?一些其他掩碼來排除數據?

在上面的響應數據示例中,我基本上不希望包含itemCategory部分。這個數據在這裏:

   "itemCategory" : { 
       "categoryCode" : "hub", 
       "groups" : [ 
        { 
         "itemCategoryId" : 74, 
         "packageId" : 206, 
         "prices" : [ 

它似乎東西的SoftLayer API改變了,當我嘗試用PHP做什麼json_encode($ catResult),其中$ catResult是SoftLayer_Product_Package :: getCategories的結果()和json_encode()函數檢測recurssion以便終止。根據我使用海報的數據,它可能看起來像一個遞歸可能存在,但它實際上重複相同的數據一次,而不是永遠。但是,也許海報很聰明,在第一次重複之後就把它關掉了,但我不知道它真的這麼做了,或者我得到的是SoftLayer的全部數據。無論如何,我認爲我只需要一種方法來不包含這個「itemCategory」值來解決問題。請幫忙。謝謝。

回答

1

這些文章與「對象口罩」可以幫助你:

https://sldn.softlayer.com/article/Object-Masks

https://sldn.softlayer.com/blog/phil/Extended-Object-Masks-taste-things-come

https://sldn.softlayer.com/article/rest

https://softlayer.github.io/php/monthtodate/

對於你的情況,你需要指定要顯示什麼樣的屬性,例如:

https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Product_Package/206/getCategories.json?objectMask=mask[id,name,groups[prices[item[id, description]]]] 

其中:… [item[id, description]] …將只顯示iddescription我的項目(但不itemCategory

+0

就像你提到的是我現在用作解決方法,但我不想列出每個項目的屬性。相反,我喜歡除了這個特定的屬性之外,還想讓所有的東西都返回,我不認爲我已經看到了指定類似的東西的方法。 – KHP

+0

你說得對,當我們使用「對象掩碼」時,有必要指定我們想要在響應中顯示的每個屬性。 – mcruz

1

試試這個

GET https://api.softlayer.com/rest/v3.1/SoftLayer_Product_Package/206/getCategories.json?objectMask=mask[id,name,categoryCode,groups[prices[item,attributes[itemPriceAttributeType]]], packageConfigurations[isRequired, orderStepId, sort]] 
+0

哇..這實際上是把「itemCategory」值設置爲null的技巧。怎麼樣?使用objectMask = mask []與objectMask =之間有區別嗎?必須有..從我可以看到我設置的objectMask(?objectMask = id; name; categoryCode; groups.prices.item; groups.prices.attributes.itemPriceAttributeType; packageConfigurations.isRequired; packageConfigurations.orderStepId; packageConfigurations.sort )與您所設置的相同,但您只是使用[]指定了不同的方式。你能否就這種行爲差異提供一些解釋? – KHP

+0

我想我看到,也許差異在於您使用v3.1而不是我使用的v3的URI。我正在使用PHP庫,我不認爲有一種方法可以指定使用v3.1 vs v3。我只是使用SoftLayer_SoapClient :: getClient(serviceName,id,API_ID,API_Key);我將如何獲得使用此v3.1的SoftLayer客戶端?我是否需要選擇更新的圖書館?有沒有選擇指定使用哪個版本?這是有點問題..但如果你知道,請告訴... – KHP

+0

有關的面具我不知道我用過的面具和你們之間的區別是什麼:(我使用的面具稱爲擴展對象掩碼https://sldn.softlayer.com/blog/phil/Extended-Object-Masks-taste-things-come。相關你的關於php的問題,當然你可以改變它當你調用getClient方法有一個參數調用在那裏您可以指定您希望使用的URL請參閱https://github.com/softlayer/softlayer-api-php-client/blob/master/src/SoapClient.php#L230。您可以將值https:/ /api.softlayer.com/soap/v3.1/ –

相關問題