我正在使用以下代码来解析一些JSON,并且我不断收到意外的JSON结果。
它最初来自here,但我无法使代码正常工作,因为第57行$status
不是一个数组,所以它抛出了与{{1}这一事实有关的错误}不是数组:
我通过添加支票来解决这个问题:
$status
现在代码运行正常,但返回的JSON如下:
56 if (is_array($status)) {}
我知道这是不正确的,因为当我在Pebble Watch上运行an application时,它意味着显示正确的数据并且显然会失败,因为{"0":null,"1":" 0"}
内没有。
据我所知,以下代码应插入0
代替$order
,但由于某种原因,它总是返回null
。
null
非常感谢任何帮助,并提前感谢您。
这是完整的代码(包括utils):
main.php
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 );
utils.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 ?>
答案 0 :(得分:1)
为了保护消费者,如果它在json字符串{"0":null,"1":" 0"}
中返回单词'null',只需执行字符串替换并将模式null替换为''。
$payload = file_get_contents('php://input');
$payload = str_replace(' null ', '""', $payload);
return json_decode($payload, true);
要修复send_response(),因此它永远不会包含'null',因此您可以在将数据编码为之前对数组进行迭代:
我喜欢数字1:
function send_response($data) {
foreach($data as $key=>$value){
$data[$key] = is_null($value) ? '' : $value;
}
$response = json_encode($data, JSON_FORCE_OBJECT);
header('Content-Type: application/json');
header('Content-Length: ' . strlen($response));
echo $response;
exit;
}