2017-09-13 229 views
-2

這裏是一個對象:如何訪問此對象的深層嵌套屬性?

{ 
    "@odata.context":"http://company/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes/Microsoft.Dynamics.CRM.StateAttributeMetadata(LogicalName,OptionSet)","value":[ 
    { 
     "LogicalName":"statecode","MetadataId":"cdc3895a-7539-41e9-966b-3f9ef805aefd","[email protected]":"http://vsevm.centralus.cloudapp.azure.com/Xypex/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes(cdc3895a-7539-41e9-966b-3f9ef805aefd)/Microsoft.Dynamics.CRM.StateAttributeMetadata/OptionSet/$entity","OptionSet":{ 
     "MetadataId":"88fa5ad0-2a4b-4281-ac9c-b4e71fb77920","HasChanged":null,"Description":{ 
      "LocalizedLabels":[ 
      { 
       "Label":"Status of the contact.","LanguageCode":1033,"IsManaged":true,"MetadataId":"fc7cf5df-8503-46e3-85b2-6c0ac68bc912","HasChanged":null 
      } 
      ],"UserLocalizedLabel":{ 
      "Label":"Status of the contact.","LanguageCode":1033,"IsManaged":true,"MetadataId":"fc7cf5df-8503-46e3-85b2-6c0ac68bc912","HasChanged":null 
      } 
     },"DisplayName":{ 
      "LocalizedLabels":[ 
      { 
       "Label":"Status","LanguageCode":1033,"IsManaged":true,"MetadataId":"37c73b95-afd6-4d25-9ded-3691db6ce56b","HasChanged":null 
      } 
      ],"UserLocalizedLabel":{ 
      "Label":"Status","LanguageCode":1033,"IsManaged":true,"MetadataId":"37c73b95-afd6-4d25-9ded-3691db6ce56b","HasChanged":null 
      } 
     },"IsCustomOptionSet":false,"IsGlobal":false,"IsManaged":true,"IsCustomizable":{ 
      "Value":true,"CanBeChanged":false,"ManagedPropertyLogicalName":"iscustomizable" 
     },"Name":"contact_statecode","OptionSetType":"State","IntroducedVersion":null,"Options":[ 
      { 
      "@odata.type":"#Microsoft.Dynamics.CRM.StateOptionMetadata","Value":0,"Label":{ 
       "LocalizedLabels":[ 
       { 
        "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null 
       } 
       ],"UserLocalizedLabel":{ 
       "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null 
       } 
      },"Description":{ 
       "LocalizedLabels":[ 

       ],"UserLocalizedLabel":null 
      },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null,"DefaultStatus":1,"InvariantName":"Active" 
      },{ 
      "@odata.type":"#Microsoft.Dynamics.CRM.StateOptionMetadata","Value":1,"Label":{ 
       "LocalizedLabels":[ 
       { 
        "Label":"Inactive","LanguageCode":1033,"IsManaged":true,"MetadataId":"4954c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null 
       } 
       ],"UserLocalizedLabel":{ 
       "Label":"Inactive","LanguageCode":1033,"IsManaged":true,"MetadataId":"4954c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null 
       } 
      },"Description":{ 
       "LocalizedLabels":[ 

       ],"UserLocalizedLabel":null 
      },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null,"DefaultStatus":2,"InvariantName":"Inactive" 
      } 
     ] 
     } 
    } 
    ] 
} 

我訪問使用JavaScript這個對象。 Id喜歡訪問此LocalizedLabel中的「標籤」的值。

"Label":{ 
       "LocalizedLabels":[ 
       { 
        "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa- 
2241-db11-898a-0007e9e17ebd","HasChanged":null 
        } 
} 
+0

開始於任何JavaScript對象,您已經將JSON解析爲.. –

+0

然後什麼?????? –

+0

你是怎麼「用JavaScript來解析它的?」 – Rafael

回答

1

訪問Javascript對象中的不同圖層是非常直接的,一旦你得到了把戲。基本上,當你想要更深入一個對象時,你使用「。」來表示。其次是密鑰名稱。 例如:

object = {"key1": { "key2": "some value"}} 
value = object.key1.key2 //returns "some value" 

這將返回與該密鑰關聯的值。如果該值是另一個對象,則可以迭代該過程。如果該值是一個數組,那麼就像在任何其他數組上一樣操作它。

再加上通常數組訪問技術:

object = {"key1": 
      { "key2": 
       [ 
        {"key3": "some value"}, 
        {"key3": "another value"} 
       ] 
     }} 
list = object.key1.key2 // returns "[ {"key3": "some value"},{"key3": "another attribute"}]" 

listElement = object.key1.key2[0] // return "{"key3": "some value"}" 

value = object.key1.key2[0].key3 // returns "some value" 

otherValue = object.key1.key2[1].key3 // returns "another value" 

編輯:正如在下面的評論指出,JSON是一個簡單的格式化,本身不是一個數據類型。你想知道的是這裏是如何操作javascript對象和數組。 (換句話說,JSON是一種將對象和/或數組作爲字符串的語法方式正確的javascript,有關更多詳細信息,請參閱here

+0

請注意,此答案中沒有JSON,只是嵌套的對象/數組。 – nnnnnn

+0

好吧,我的錯誤。編輯我的答案,謝謝你指出。 –

相關問題