Leonardo Amigoni
Leonardo Amigoni

Reputation: 2317

Javascript extract data from dictionary.

This might seem very basic but I don't get it.

I have the following data from a dictionary.

To extract ColorModel I just do object.ColorModel
No problem or issues here.

But how do I extract "{Exif}"? I can't type 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;

};

}

Upvotes: 0

Views: 3290

Answers (3)

LetterEh
LetterEh

Reputation: 26696

Try object["{Exif"}"];

No period. This will allow you to reference invalid JS names (like this case) or reserved words like "class" or "in".

Upvotes: 2

jfriend00
jfriend00

Reputation: 707308

Your code is not valid javascript. This is the proper form using colons for declaration, not equals and commas to separate multiple properties, not semi-colons:

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

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

Then, to reference a property name that contains characters that are not legal in a javascript symbol, you can use the ["{EXIF}"] notation.

Upvotes: 2

Dan Herbert
Dan Herbert

Reputation: 103407

Treat the object like a hash table. All object properties can be used as hash strings in JavaScript.

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

Upvotes: 1

Related Questions