-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBSJson.php
75 lines (71 loc) · 2.03 KB
/
LBSJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Coypright © 2013 Neilsen Chan. All rights reserved.
* Author: chenjinlong
* Date: 3/30/13
* Time: 4:26 PM
* Description: LBSJson.php
*/
class LBSJson
{
/**
* 整理JSON字符串的呈现形式,使之可视化更佳
*
* @author chenjinlong 20130329
* @param $json
* @return string
*/
public static function tidyJsonFormat($json)
{
$tab = " ";
$newJson = "";
$indentLevel = 0;
$inString = false;
$len = strlen($json);
for($c = 0; $c < $len; $c++) {
$char = $json[$c];
switch($char) {
case '{':
case '[':
if(!$inString) {
$newJson .= $char . "\n" . str_repeat($tab, $indentLevel+1);
$indentLevel++;
} else {
$newJson .= $char;
}
break;
case '}':
case ']':
if(!$inString) {
$indentLevel--;
$newJson .= "\n" . str_repeat($tab, $indentLevel) . $char;
} else {
$newJson .= $char;
}
break;
case ',':
if(!$inString) {
$newJson .= ",\n" . str_repeat($tab, $indentLevel);
} else {
$newJson .= $char;
}
break;
case ':':
if(!$inString) {
$newJson .= ": ";
} else {
$newJson .= $char;
}
break;
case '"':
if($c > 0 && $json[$c-1] != '\\') {
$inString = !$inString;
}
default:
$newJson .= $char;
break;
}
}
return $newJson;
}
}