返回字符串的子元素

时间:2013-10-17 21:30:57

标签: php arrays json string

下面有一个字符串输出。例如,我如何仅返回文件名?类型是字符串。我以为它是json,但是当我使用gettype时,它返回string。

[{ "SourceFile": "example.jpg", "ExifTool": { "ExifToolVersion": 9.37 }, "File": { "FileName": "example.jpg", "Directory": ".", "FileSize": "323 kB", "FileModifyDate": "2013:10:17 15:10:21+00:00", "FileAccessDate": "2013:10:03 19:38:58+00:00", "FileInodeChangeDate": "2013:10:17 15:10:21+00:00", "FilePermissions": "rwxrwxrwx", "FileType": "JPEG", "MIMEType": "image/jpeg", "ImageWidth": 1000, "ImageHeight": 658, "EncodingProcess": "Progressive DCT, Huffman coding", "BitsPerSample": 8, "ColorComponents": 3, "YCbCrSubSampling": "YCbCr4:4:4 (1 1)" }, "JFIF": { "JFIFVersion": 1.02, "ResolutionUnit": "None", "XResolution": 100, "YResolution": 100 }, "Ducky": { "Quality": "95%" }, "XMP": { "XMPToolkit": "Adobe XMP Core 4.2.2-c063 53.352624, 2008/07/30-18:12:18 ", "Make": "Canon", "Model": "Canon EOS 40D", "XResolution": 240, "YResolution": 240, "ResolutionUnit": "inches", "ExifVersion": "0221", "ExposureTime": "1/1000", "ShutterSpeedValue": "1/1000", "FNumber": 8.0, "ApertureValue": 8.0, "ExposureProgram": "Aperture-priority AE", "DateTimeOriginal": "2013:09:27 13:03:40.00+01:00", "DateTimeDigitized": "2013:09:27 13:03:40.00+01:00", "ExposureCompensation": "-2/3", "MaxApertureValue": 4.0, "SubjectDistance": "5.19 m", "MeteringMode": "Multi-segment", "FocalLength": "500.0 mm", "CustomRendered": "Normal", "ExposureMode": "Auto", "WhiteBalance": "Auto", "SceneCaptureType": "Standard", "FocalPlaneXResolution":
4438.35616438356, "FocalPlaneYResolution": 4445.96912521441, "FocalPlaneResolutionUnit": "inches", "ExifImageWidth": 3888, "ExifImageHeight": 2592, "ModifyDate": "2013:09:27 13:03:40.00+01:00", "CreateDate": "2013:09:27 13:03:40.00+01:00", "Rating": 0, "MetadataDate": "2013:09:27 13:03:40.00+01:00", "SerialNumber": 1130502837, "LensInfo": "500mm f/?", "Lens": "EF500mm f/4L IS USM", "LensID": 143, "ImageNumber": 0, "FlashCompensation": 0, "OwnerName": "Derek Lees WS66JU", "Firmware": "1.1.1", "Format": "image/tiff", "PreservedFileName": "IMG_8987.CR2", "SidecarForExtension": "CR2", "ColorMode": "RGB", "ISO": 400, "FlashFired": false, "FlashReturn": "No return detection", "FlashMode": "Off", "FlashFunction": false, "FlashRedEyeMode": false, "Creator": ["Derek Lees WS66JU","Derek Lees"], "Rights": "© Derek Lees 2013", "HistoryAction": "converted", "HistoryParameters": "from image/tiff to image/jpeg" }, "APP14": { "DCTEncodeVersion": 100, "APP14Flags0": "[14], Encoded with Blend=1 downsampling", "APP14Flags1": "(none)", "ColorTransform": "YCbCr" }, "Composite": { "Aperture": 8.0, "Flash": "Off, Did not fire", "ImageSize": "1000x658", "ScaleFactor35efl": 1.6, "ShutterSpeed": "1/1000", "CircleOfConfusion": "0.019 mm", "DOF": "0.03 m (5.18 -
5.20)", "FOV": "2.5 deg", "FocalLength35efl": "500.0 mm (35 mm equivalent: 809.4 mm)", "HyperfocalDistance": "1683.65 m", "LensID": "Canon EF 500mm f/4L IS", "LightValue": 14.0 } }] jsonelementsimagewidthworkigntypestringiii1The string '' was not found in the string ''The string '' was not found in the string ''The string '' was not found in the string ''data hack please[{ mastercount0counteroriginal1

2 个答案:

答案 0 :(得分:0)

JSON不是一种类型,它是一种序列化对象的方法。 See here。因此,它总是一个字符串。

如果您希望字符串始终是有效的JSON,那么您可以使用json_decode

要转到SourceFile元素,您可以使用:

$decoded = json_decode($data, true);
$sourceFile = $decoded[0]['SourceFile'];

[]表示JSON中的数组。

答案 1 :(得分:0)

PHP会将JSON字符串视为“string”类型...尝试通过json_decode()运行它:

$data = json_decode($that_string_from_above);