http://example.com/doctracklog/20140827091936
我需要从URI ff解析JSON,例如上面的示例。
内置URI(http://example.com/doctracklog/)+(textbox1)后跟textbox1
;
只有textbox1
的值会发生变化。
可以是Android,JavaScript等。
答案 0 :(得分:0)
您的问题过于宽泛,但我会发布一个通用解决方案 PHP / JQUERY / JS / AJAX / .HTACCESS
.htaccess
文件
此重写规则将使友好网址/doctracklog/{some-value}
生效。
Options Indexes FollowSymLinks
RewriteEngine on
RewriteRule doctracklog/(.*)/? doctracklog.php?id=$1
doctracklog.php
文件此代码将读取json对象并显示特定错误的用户信息
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
<!-- javascript/jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<div id="holder"></div>
</head>
<body>
<script>
$.ajax({
url: '/json.php',
type: 'POST',
data: {
'id': '<?php echo !empty($_GET['id']) ? $_GET['id'] : "";?>'
},
success: function (response) {
if (response.status !== 'Error') {
console.log(response);
alert(response.user.name + "-" + response.user.age + "-" + response.user.gender);
} else {
alert(response.message);
}
},
error: function () {
alert("error");
}
});
</script>
</body>
</html>
json.php
文件
此文件将根据传递的值(http://example.com/doctracklog/ 20140827091936 )设置将显示哪个用户,并返回一个json对象
<?php
//this is the number you are passing in your URI. For ex. http://example.com/doctracklog/20140827091936
$userId = $_POST['id'] ? $_POST['id'] : "";
//This is a dummy array example, so we can use userId as its index
if(!empty($userId)) {
$users = array(
'20140827091936' => array(
'name' => 'John',
'age' => '20',
'gender' => 'male'
),
'20140920081120' => array(
'name' => 'Susan',
'age' => '32',
'gender' => 'female'
)
);
if(array_key_exists($userId,$users)) {
$json = array('status' => 'OK', 'user' => $users[$userId]);
} else {
$json = array('status' => 'Error','message' => 'Id does not exist');
}
} else {
$json = array('status' => 'Error','message' => 'You did not pass any id');
}
//return json object
header('Content-Type: application/json');
echo json_encode($json);
以下是一些可能的例子。
没有ID :http://example.com/doctracklog/
错误的ID :http://example.com/doctracklog/111111111
正确的ID :http://example.com/doctracklog/20140827091936
这些是示例的结果。