【问题标题】:Parsing JSON values from media file metadata on OSX从 OSX 上的媒体文件元数据解析 JSON 值
【发布时间】:2017-08-23 01:49:43
【问题描述】:

这是我第一次尝试 Swift 3 和 Xcode 8.3.3。我正在尝试通过命令行应用程序 Exiftool 解析从文件中提取的 JSON 元数据。

    let task = Process()
    let filePath = url.path
    let etPath = "/usr/local/bin/exiftool"

    task.launchPath = etPath
    task.arguments = ["-a", "-g1", "-json", filePath]

    let pipe = Pipe()
    task.standardOutput = pipe

    task.launch()

    // Get the data
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
    let mydata = output?.data(using: String.Encoding.utf8.rawValue)!

    do {
        let myJson = try JSONSerialization.jsonObject(with: mydata!, options: []) as AnyObject

        if let XMPdc = myJson["XMP-dc"] as AnyObject? {

            if let creator = XMPdc["Creator"] as! NSArray? {
                print(creator)
            }
        }
    } catch let error as NSError {
          print(error)
    }

脚本的第一部分运行良好,允许我将 JSON 数据放入变量 myJson。如果我打印出那个变量,我会得到这个:

(
    {
    Composite =         {
        ImageSize = 100x100;
        Megapixels = "0.01";
    };
    ExifIFD =         {
        ColorSpace = Uncalibrated;
        ExifImageHeight = 100;
        ExifImageWidth = 100;
    };
    ExifTool =         {
        ExifToolVersion = "10.61";
    };
    File =         {
        ExifByteOrder = "Little-endian (Intel, II)";
        FileType = TIFF;
        FileTypeExtension = tif;
        MIMEType = "image/tiff";
    };
    "ICC-header" =         {
        CMMFlags = "Not Embedded, Independent";
        ColorSpaceData = "RGB ";
        ConnectionSpaceIlluminant = "0.9642 1 0.82487";
        DeviceAttributes = "Reflective, Glossy, Positive, Color";
        DeviceManufacturer = KODA;
        DeviceModel = ROMM;
        PrimaryPlatform = "Microsoft Corporation";
        ProfileCMMType = KCMS;
        ProfileClass = "Display Device Profile";
        ProfileConnectionSpace = "XYZ ";
        ProfileCreator = KODA;
        ProfileDateTime = "1998:12:01 18:58:21";
        ProfileFileSignature = acsp;
        ProfileID = 0;
        ProfileVersion = "2.1.0";
        RenderingIntent = "Media-Relative Colorimetric";
    };
    "ICC_Profile" =         {
        BlueMatrixColumn = "0.03134 9e-05 0.82491";
        BlueTRC = "(Binary data 14 bytes, use -b option to extract)";
        DeviceMfgDesc = KODAK;
        DeviceModelDesc = "Reference Output Medium Metric(ROMM)  ";
        GreenMatrixColumn = "0.13519 0.71188 0";
        GreenTRC = "(Binary data 14 bytes, use -b option to extract)";
        MakeAndModel = "(Binary data 40 bytes, use -b option to extract)";
        MediaWhitePoint = "0.9642 1 0.82489";
        ProfileCopyright = "Copyright (c) Eastman Kodak Company, 1999, all rights reserved.";
        ProfileDescription = "ProPhoto RGB";
        RedMatrixColumn = "0.79767 0.28804 0";
        RedTRC = "(Binary data 14 bytes, use -b option to extract)";
    };
    IFD0 =         {
        Artist = Autore;
        BitsPerSample = "16 16 16";
        Compression = LZW;
        ImageDescription = "Creator: Test ; Date: 1900";
        ImageHeight = 100;
        ImageWidth = 100;
        ModifyDate = "2017:08:10 10:58:42";
        Orientation = "Horizontal (normal)";
        PhotometricInterpretation = RGB;
        PlanarConfiguration = Chunky;
        ResolutionUnit = inches;
        RowsPerStrip = 100;
        SamplesPerPixel = 3;
        Software = "Adobe Photoshop CC 2015 (Macintosh)";
        StripByteCounts = 1037;
        StripOffsets = 11450;
        SubfileType = "Full-resolution Image";
        XResolution = 300;
        YCbCrPositioning = "Co-sited";
        YResolution = 300;
    };
    Photoshop =         {
        DisplayedUnitsX = inches;
        DisplayedUnitsY = inches;
        GlobalAltitude = 30;
        GlobalAngle = 90;
        HasRealMergedData = Yes;
        IPTCDigest = 00000000000000000000000000000000;
        PhotoshopThumbnail = "(Binary data 557 bytes, use -b option to extract)";
        PixelAspectRatio = 1;
        PrintPosition = "0 0";
        PrintScale = 1;
        PrintStyle = Centered;
        ReaderName = "Adobe Photoshop CC 2015";
        SlicesGroupName = "";
        "URL_List" =             (
        );
        WriterName = "Adobe Photoshop";
        XResolution = 300;
        YResolution = 300;
    };
    "XMP-dc" =         {
        Creator = Autore;
        Description = "Creator: Test ; Date: 1900";
        Format = "image/tiff";
        Publisher = "-";
        Rights = "-";
        Subject = "-";
        Title = tite;
    };
    "XMP-pdf" =         {
        Producer = "-";
    };
    "XMP-photoshop" =         {
        ColorMode = RGB;
        ICCProfileName = "ProPhoto RGB";
    };
    "XMP-x" =         {
        XMPToolkit = "Image::ExifTool 10.53";
    };
    "XMP-xmp" =         {
        CreateDate = 1900;
        CreatorTool = "Adobe Photoshop CC 2015 (Macintosh)";
        MetadataDate = "2017:08:10 10:58:42-04:00";
        ModifyDate = "2017:08:10 10:58:42-04:00";
        ThumbnailFormat = "-";
        ThumbnailImage = "(Binary data 48 bytes, use -b option to extract)";
    };
    "XMP-xmpRights" =         {
        Marked = 1;
    };
    }
)

但是,我不明白如何正确解析数据以存储特定值,假设对应于我的变量 creator 中的对象 { "XMP-dc" = {Creator = Autore } } 的值。

我做错了什么?

【问题讨论】:

    标签: json swift macos xcode8 exiftool


    【解决方案1】:

    尝试将你的 json 和 xmp-dc 降级到字典中

    let myJson = try JSONSerialization.jsonObject(with: mydata!, options: []) as? [String: Any]
    
    if let XMPdc = myJson?["XMP-dc"] as? [String: Any?] {
    
                //print that dictionary to be sure that al is correct
               print(XMPdc)
    
              //I'm not sure that the value Creator is an Array, honestly I don't understand what is the type of that object, but anyway, few fixes for your code
                if let creator = XMPdc["Creator"] as? NSArray {
                    print(creator)
                }
            }
    

    同样,我不确定变量 Creator 的类型,但至少你会在进行 print(XMPdc) 时知道这一点,然后你可以将创建者的向下转换更改为字符串或其他任何内容。 如果您需要什么,请告诉我

    【讨论】:

    • 亲爱的 Woof,非常感谢您的帮助。我按照您的建议尝试了print(XMPdc)solution,但调试区域中没有出现任何内容……
    • 那么 myJson 呢?你能打印这个变量吗?
    • 那个变量打印出来很完美(你会在我的第一篇文章的第二部分找到存储在那个变量中的 JSON)
    • 是的,但我只是想确定 myJson 不是 nil 并且打印的结构与您的代码中的相同。并在这里分享一张打印myJson的图片?["XMP-dc"] 不应该是nil,否则解析有问题
    猜你喜欢
    • 2021-05-29
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多