J'utilise le code suivant pour analyser certains fichiers JSON et j'obtiens un résultat JSON inattendu.Charge utile JSON attendue ne renvoyant pas la charge utile JSON correcte
Il est originaire de here, mais je ne pouvais pas obtenir ce code de travail, car la ligne 57 $status
n'a pas été un tableau il vomissait une erreur liée au fait que $status
était pas un tableau:
I fixe en ajoutant un chèque:
56 if (is_array($status)) {}
maintenant, le code fonctionne très bien, mais le JSON retourné est la suivante:
{"0":null,"1":" 0"}
Je sais que ce n'est pas correct, parce que quand je cours an application sur mon Pebble Watch, il est destiné à afficher les données correctes et échoue évidemment parce qu'il n'y en a aucun dans 0
.
Pour autant que je peux dire le code suivant devrait insérer $order
en place de null
mais il est toujours le retour null
plutôt pour une raison quelconque.
35 // Grab the tube status and the incoming payload.
36 $tube_data = json_decode(file_get_contents($API_URL), true);
37 $payload = get_payload();
38
39 $order = $payload['0'];
40
41 // Start building the response.
42 $response = array(
43 '0' => $order,
44 '1' => ''
45 );
Toute aide serait grandement appréciée et merci d'avance.
Voici le code complet (y compris utils):
main.php
1 <?php
2
3 // Include my shared functions.
4 include_once($_SERVER['DOCUMENT_ROOT'] . '/utils.php');
5
6 // The URL of the Tube Status API.
7 $API_URL = 'http://api.tubeupdates.com/?method=get.status&format=json';
8
9 // Mapping between shortcode and line name.
10 $line_codes = array(
11 'BL' => 'bakerloo',
12 'CE' => 'central',
13 'CI' => 'circle',
14 'DI' => 'district',
15 'DL' => 'docklands',
16 'HC' => 'hammersmithcity',
17 'JL' => 'jubilee',
18 'ME' => 'metropolitan',
19 'NO' => 'northern',
20 'OV' => 'overground',
21 'PI' => 'piccadilly',
22 'VI' => 'victoria',
23 'WC' => 'waterloocity'
24 );
25
26 // Mapping between errors and numbers
27 $statuses = array(
28 'good service' => 1,
29 'part closure' => 2,
30 'minor delays' => 4,
31 'severe delays' => 8,
32 'part suspended' => 16
33 );
34
35 // Grab the tube status and the incoming payload.
36 $tube_data = json_decode(file_get_contents($API_URL), true);
37 $payload = get_payload();
38
39 $order = $payload['0'];
40
41 // Start building the response.
42 $response = array(
43 '0' => $order,
44 '1' => ''
45 );
46
47 // Split the ordering string into the 2 character line short codes.
48 $lines = str_split($order, 2);
49 foreach ($lines as $pos => $line) {
50
51 // Get the status for the line given its short code.
52 $status = get_status_by_id($line_codes[$line]);
53
54 // Do bitwise ORs on the status number to build it up
55 $status_number = 0;
56 if (is_array($status)) {
57 foreach ($status as $st) {
58 $status_number = $status_number |= $statuses[$st];
59 }
60 }
61
62 // Append the status code to the response string.
63 $response['1'] .= str_pad($status_number, 2, ' ', STR_PAD_LEFT);
64 }
65
66 // Send the response.
67 send_response($response);
68
69 // Takes a line code (not shortcode) and returns an array of its current status.
70 function get_status_by_id($id) {
71 global $tube_data;
72
73 foreach ($tube_data['response']['lines'] as $index => $line) {
74 if ($line['id'] == $id) {
75 return explode(', ', $line['status']);
76 }
77 }
78 return NULL;
79 }
80
81 ?>
utils.php
<?php
function get_payload() {
return json_decode(file_get_contents('php://input'), true);
}
function send_response($data) {
$response = json_encode($data, JSON_FORCE_OBJECT);
header('Content-Type: application/json');
header('Content-Length: ' . strlen($response));
echo $response;
exit;
}
?>
double possible de [la charge utile JSON ne pas retourner correctement Expected] (http://stackoverflow.com/questions/18139137/expected-json-payload-not-return-correctement) – andrewsi
@gotnull est-ce que ma réponse ci-dessous résout le problème? – Ray