Odczytanie danych

1

Cześć,
Potrzebuję tylko odczytać pola name oraz confidence. Podpowie ktoś jak to zrobić? Z góry dziękuje za pomoc.

"Labels": [ { "Name": "Car", "Confidence": 99.81039428710938, "Instances": [ { "BoundingBox": { "Width": 0.7304845452308655, "Height": 0.5142102241516113, "Left": 0.17355753481388092, "Top": 0.3424329459667206 }, "Confidence": 99.81039428710938 }, { "BoundingBox": { "Width": 0.0816001445055008, "Height": 0.20880360901355743, "Left": 0.001656003762036562, "Top": 0.13974758982658386 }, "Confidence": 97.28746032714844 } ], "Parents": [ { "Name": "Transportation" }, { "Name": "Vehicle" } ] }, { "Name": "Transportation", "Confidence": 99.81039428710938, "Instances": [], "Parents": [] }, { "Name": "Vehicle", "Confidence": 99.81039428710938, "Instances": [], "Parents": [ { "Name": "Transportation" } ] }, { "Name": "Wheel", "Confidence": 98.66731262207031, "Instances": [ { "BoundingBox": { "Width": 0.04912351071834564, "Height": 0.1656130850315094, "Left": 0.8461058735847473, "Top": 0.4779161512851715 }, "Confidence": 82.66529846191406 } ], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Machine", "Confidence": 98.66731262207031, "Instances": [], "Parents": [] }, { "Name": "Tire", "Confidence": 98.41291809082031, "Instances": [], "Parents": [] }, { "Name": "Spoke", "Confidence": 98.1344223022461, "Instances": [], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Alloy Wheel", "Confidence": 97.73593139648438, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Spoke" }, { "Name": "Machine" } ] }, { "Name": "Car Wheel", "Confidence": 96.28256225585938, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Tire" }, { "Name": "Machine" } ] }, { "Name": "Monitor", "Confidence": 88.2918930053711, "Instances": [], "Parents": [] } ],

1

przecież to jest zwykły json - użyj json_decode a potem to już chyba potrafisz dobrać się do danych

1
abrakadaber napisał(a):

przecież to jest zwykły json - użyj json_decode a potem to już chyba potrafisz dobrać się do danych

Robię tak, ale zwraca mi to pustą stronę :(

$obj = json_decode($result, true);
echo $obj['Name'];
1

Użyj var_dump na całym $obj. I zobaczysz wtedy, co jest zwracane.

0
marnit napisał(a):

Robię tak, ale zwraca mi to pustą stronę :(

$obj = json_decode($result, true);
echo $obj['Name'];

Brak nawiasów klamerkowych!

print_r(json_decode('{"Labels": [ { "Name": "Car", "Confidence": 99.81039428710938, "Instances": [ { "BoundingBox": { "Width": 0.7304845452308655, "Height": 0.5142102241516113, "Left": 0.17355753481388092, "Top": 0.3424329459667206 }, "Confidence": 99.81039428710938 }, { "BoundingBox": { "Width": 0.0816001445055008, "Height": 0.20880360901355743, "Left": 0.001656003762036562, "Top": 0.13974758982658386 }, "Confidence": 97.28746032714844 } ], "Parents": [ { "Name": "Transportation" }, { "Name": "Vehicle" } ] }, { "Name": "Transportation", "Confidence": 99.81039428710938, "Instances": [], "Parents": [] }, { "Name": "Vehicle", "Confidence": 99.81039428710938, "Instances": [], "Parents": [ { "Name": "Transportation" } ] }, { "Name": "Wheel", "Confidence": 98.66731262207031, "Instances": [ { "BoundingBox": { "Width": 0.04912351071834564, "Height": 0.1656130850315094, "Left": 0.8461058735847473, "Top": 0.4779161512851715 }, "Confidence": 82.66529846191406 } ], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Machine", "Confidence": 98.66731262207031, "Instances": [], "Parents": [] }, { "Name": "Tire", "Confidence": 98.41291809082031, "Instances": [], "Parents": [] }, { "Name": "Spoke", "Confidence": 98.1344223022461, "Instances": [], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Alloy Wheel", "Confidence": 97.73593139648438, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Spoke" }, { "Name": "Machine" } ] }, { "Name": "Car Wheel", "Confidence": 96.28256225585938, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Tire" }, { "Name": "Machine" } ] }, { "Name": "Monitor", "Confidence": 88.2918930053711, "Instances": [], "Parents": [] } ],}',1));

Źle to skopiowałeś i dlatego nie działa.

0

cały wynik z API

Model Data ---------- Data can be retrieved from the model object using the get() method of the model (e.g., `$result->get($key)`) or "accessing the result like an associative array (e.g. `$result['key']`). You can also execute JMESPath expressions on the result data using the search() method. { "Labels": [ { "Name": "Car", "Confidence": 99.81039428710938, "Instances": [ { "BoundingBox": { "Width": 0.7304845452308655, "Height": 0.5142102241516113, "Left": 0.17355753481388092, "Top": 0.3424329459667206 }, "Confidence": 99.81039428710938 }, { "BoundingBox": { "Width": 0.0816001445055008, "Height": 0.20880360901355743, "Left": 0.001656003762036562, "Top": 0.13974758982658386 }, "Confidence": 97.28746032714844 } ], "Parents": [ { "Name": "Transportation" }, { "Name": "Vehicle" } ] }, { "Name": "Vehicle", "Confidence": 99.81039428710938, "Instances": [], "Parents": [ { "Name": "Transportation" } ] }, { "Name": "Transportation", "Confidence": 99.81039428710938, "Instances": [], "Parents": [] }, { "Name": "Wheel", "Confidence": 98.66729736328125, "Instances": [ { "BoundingBox": { "Width": 0.04912351071834564, "Height": 0.1656130850315094, "Left": 0.8461058735847473, "Top": 0.4779161512851715 }, "Confidence": 82.66529846191406 } ], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Machine", "Confidence": 98.66729736328125, "Instances": [], "Parents": [] }, { "Name": "Tire", "Confidence": 98.41289520263672, "Instances": [], "Parents": [] }, { "Name": "Spoke", "Confidence": 98.13444519042969, "Instances": [], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Alloy Wheel", "Confidence": 97.7359390258789, "Instances": [], "Parents": [ { "Name": "Machine" }, { "Name": "Wheel" }, { "Name": "Spoke" } ] }, { "Name": "Car Wheel", "Confidence": 96.28250885009766, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Tire" }, { "Name": "Machine" } ] }, { "Name": "Monitor", "Confidence": 88.29204559326172, "Instances": [], "Parents": [] } ], "LabelModelVersion": "2.0", "@metadata": { "statusCode": 200, "effectiveUri": "https:\/\/rekognition.us-east-1.amazonaws.com", "headers": { "content-type": "application\/x-amz-json-1.1", "date": "Mon, 23 Mar 2020 10:51:28 GMT", "x-amzn-requestid": "7142a983-fbaf-41c8-a114-425c0e6a7fa4", "content-length": "1496", "connection": "keep-alive" }, "transferStats": { "http": [ [] ] } } }

0
<?php
$result = 'Model Data ---------- Data can be retrieved from the model object using the get() method of the model (e.g., `$result->get($key)`) or "accessing the result like an associative array (e.g. `$result[\'key\']`). You can also execute JMESPath expressions on the result data using the search() method. { "Labels": [ { "Name": "Car", "Confidence": 99.81039428710938, "Instances": [ { "BoundingBox": { "Width": 0.7304845452308655, "Height": 0.5142102241516113, "Left": 0.17355753481388092, "Top": 0.3424329459667206 }, "Confidence": 99.81039428710938 }, { "BoundingBox": { "Width": 0.0816001445055008, "Height": 0.20880360901355743, "Left": 0.001656003762036562, "Top": 0.13974758982658386 }, "Confidence": 97.28746032714844 } ], "Parents": [ { "Name": "Transportation" }, { "Name": "Vehicle" } ] }, { "Name": "Vehicle", "Confidence": 99.81039428710938, "Instances": [], "Parents": [ { "Name": "Transportation" } ] }, { "Name": "Transportation", "Confidence": 99.81039428710938, "Instances": [], "Parents": [] }, { "Name": "Wheel", "Confidence": 98.66729736328125, "Instances": [ { "BoundingBox": { "Width": 0.04912351071834564, "Height": 0.1656130850315094, "Left": 0.8461058735847473, "Top": 0.4779161512851715 }, "Confidence": 82.66529846191406 } ], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Machine", "Confidence": 98.66729736328125, "Instances": [], "Parents": [] }, { "Name": "Tire", "Confidence": 98.41289520263672, "Instances": [], "Parents": [] }, { "Name": "Spoke", "Confidence": 98.13444519042969, "Instances": [], "Parents": [ { "Name": "Machine" } ] }, { "Name": "Alloy Wheel", "Confidence": 97.7359390258789, "Instances": [], "Parents": [ { "Name": "Machine" }, { "Name": "Wheel" }, { "Name": "Spoke" } ] }, { "Name": "Car Wheel", "Confidence": 96.28250885009766, "Instances": [], "Parents": [ { "Name": "Wheel" }, { "Name": "Tire" }, { "Name": "Machine" } ] }, { "Name": "Monitor", "Confidence": 88.29204559326172, "Instances": [], "Parents": [] } ], "LabelModelVersion": "2.0", "@metadata": { "statusCode": 200, "effectiveUri": "https:\/\/rekognition.us-east-1.amazonaws.com", "headers": { "content-type": "application\/x-amz-json-1.1", "date": "Mon, 23 Mar 2020 10:51:28 GMT", "x-amzn-requestid": "7142a983-fbaf-41c8-a114-425c0e6a7fa4", "content-length": "1496", "connection": "keep-alive" }, "transferStats": { "http": [ [] ] } } }';
$json = substr($result,strpos($result,"{"),strrpos($result,"}"));
print_r(json_decode($json,1));
?>

1 użytkowników online, w tym zalogowanych: 0, gości: 1