Javascript从字典中提取数据。

时间:2012-08-18 19:44:11

标签: javascript dictionary

这看起来很基本,但我不明白。

我从字典中获得以下数据。

要提取ColorModel,我只需执行 object.ColorModel
这里没问题。

但是如何提取“{Exif}”?我无法输入 object。“{Exif}”

{

ColorModel = RGB;

DPIHeight = 72;

DPIWidth = 72;

Depth = 8;

Orientation = 1;

PixelHeight = 3000;

PixelWidth = 4000;

"{Exif}" =     {

    ApertureValue = "2.96875";

    ColorSpace = 1;

    ComponentsConfiguration =         (

        0,

        0,

        0,

        1

    );

    CompressedBitsPerPixel = 3;

    CustomRendered = 0;

    DateTimeDigitized = "2012:03:22 13:17:00";

    DateTimeOriginal = "2012:03:22 13:17:00";

    DigitalZoomRatio = 1;

    ExifVersion =         (

        2,

        2

    );

    ExposureBiasValue = 0;

    ExposureMode = 0;

    ExposureTime = "0.01666666753590107";

    FNumber = "2.799999952316284";

    Flash = 24;

    FlashPixVersion =         (

        1,

        0

    );

    FocalLength = 5;

    FocalPlaneResolutionUnit = 2;

    FocalPlaneXResolution = "16393.4453125";

    FocalPlaneYResolution = "16393.4453125";

    ISOSpeedRatings =         (

        200

    );

    MaxApertureValue = "2.96875";

    MeteringMode = 5;

    PixelXDimension = 4000;

    PixelYDimension = 3000;

    SceneCaptureType = 0;

    SensingMethod = 2;

    ShutterSpeedValue = "5.90625";

    WhiteBalance = 0;

};

"{TIFF}" =     {

    DateTime = "2012:03:26 21:00:45";

    HostComputer = "Mac OS X 10.7.3";

    Make = Canon;

    Model = "Canon PowerShot SD940 IS";

    Orientation = 1;

    ResolutionUnit = 2;

    Software = "QuickTime 7.7.1";

    XResolution = 72;

    YResolution = 72;

    "_YCbCrPositioning" = 2;

};

}

3 个答案:

答案 0 :(得分:2)

您的代码无效javascript。这是使用冒号进行声明的正确形式,而不是使用等号和逗号分隔多个属性,而不是分号:

var data = {
    ColorModel: "RGB",
    DPIHeight: 72,
    DPIWidth: 72,
    "{Exif}": {
        ApertureValue:"2.96875"
    }
}

var dpiHeight = data.DPIHeight;
var aperture = data["{EXIF}"].ApertureValue;

然后,要引用包含javascript符号中不合法字符的属性名称,可以使用["{EXIF}"]表示法。

答案 1 :(得分:2)

尝试对象[“{Exif”}“];

没有时期。 这将允许您引用无效的JS名称(如本案例)或保留字,如“class”或“in”。

答案 2 :(得分:1)

将对象视为哈希表。所有对象属性都可以用作JavaScript中的哈希字符串。

var value = object["{Exif}"];