如何访问此对象的深层嵌套属性?

时间:2017-09-13 02:36:36

标签: javascript

这是一个对象:

{
  "@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","OptionSet@odata.context":"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访问此对象。我想在此LocalizedLabel中访问“Label”的值。

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

1 个答案:

答案 0 :(得分: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