我正在尝试将用户名和密码附加到Json文件中。我花了数小时来拖曳该站点,并去了许多通常可靠的教程站点(Derek Banas,新波士顿),以一种确定的方式来操纵,附加和解析Json文件,但令我惊讶的是,我还没有找到一个。
所以我希望有人能够为我解决这个问题。到目前为止,我已经设法覆盖了我的Json文件中的数据,将数据追加到末尾,但没有以对象的形式。我的Json文件如下所示:
AJAX:
{
"LogIns":[
{
"Username":"Mike",
"password":"123"
},
{
"Username":"Farrah",
"password":"123"
},
{
"Username":"John",
"password": "123"
}
]
}
我的HTML和AJAX看起来像这样:
HTML / AJAX:
<body>
<center>
<fieldset>
<legend>Please register before playing</legend>
<form>
Username: <br>
<input type="text" placeholder="Enter a Username" id="username1" name="username"><br>
Password: <br>
<input type="password" placeholder="Enter a password" id="password" name="password"><br>
<input type="submit" value="Submit" onclick="return checkLogin();">
</form>
</fieldset>
</center>
<br>
<br>
<div id="PHPid">
<script>
var usernamePassed = "";
var userPassword = "";
function checkLogin(){
usernamePassed = document.getElementById("username1").value;
userPassword = document.getElementById("password").value;
callAJAX();
return false;
}
function callAJAX(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange=function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//location.href="gameOption.html";
console.log(xhttp.responseText);
}
}
xhttp.open("POST", "reg.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=" + usernamePassed + "&password="+ userPassword);
}
</script>
PHP文件如下所示:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$str = file_get_contents('logins.json'); // Save contents of file into a variable
$json = json_decode($str, true); // decode the data and set it to recieve data asynchronosly - store in $json
array_push($data, array('Username' => $username, "password" => $password));
file_put_contents('logins.json', json_encode($json));
?>
我正在尝试添加数据,以便下次有人注册它时,我的Json文件将如下所示:
JSON;
{
"LogIns":[
{
"Username":"Mike",
"password":"123"
},
{
"Username":"Farrah",
"password":"123"
},
{
"Username":"John",
"password": "123"
},
{
"Username":"Alfred",
"password": "123"
}
]
}
使用上面的代码,没有任何内容被打印到控制台。
我也曾在我的PHP中尝试过此操作:
$username = $_POST['username'];
$password = $_POST['password'];
$str = file_get_contents('logins.json'); // Save contents of file into a variable
$json = json_decode($str, true); // decode the data and set it to recieve data asynchronosly - store in $json
$data = array();
array_push($data, array('Username' => $username, "password" => $password));
file_put_contents('logins.json', json_encode($json));
它也不会向控制台输出任何内容,但是会将Json文件更新为如下所示:
{"LogIns":[
{"Username":"Mike",
"password":"123"},
{"Username":"Farrah",
"password":"123"},
{"Username":"John",
"password":"123"},
{"Username":"Alfred",
"password":"123"},
"Username:","mike",
"password:","123"
]
}
如果有人告诉我这很好,但是如果他们能清除覆盖整个文件的方法,像上面一样在子文件中附加一个对象,并从Json文件中删除一个对象,那就太好了!
我敢肯定这并不困难,但是我似乎找不到直接的答案。
提前致谢!
您在错误的位置追加内容。
您应该将新值附加到内部的“ LogIns”数组中,但是要附加到其容器中。
<?php
$string = ' {
"LogIns":[
{
"Username":"Mike","password":"123"
},
{
"Username":"Farrah","password":"123"
},
{
"Username":"John","password": "123"
}
]
}';
$array = json_decode($string,true);
$extra = [
'Username' => 'John',
'password' => 'doe123'
];
$array['LogIns'][] = $extra;
print_r($array);
将会很好地输出:
Array
(
[LogIns] => Array
(
[0] => Array
(
[Username] => Mike
[password] => 123
)
[1] => Array
(
[Username] => Farrah
[password] => 123
)
[2] => Array
(
[Username] => John
[password] => 123
)
[3] => Array
(
[Username] => John
[password] => doe123
)
)
)
如果json_encode
对最终数组执行a操作,则将获得以下内容
{
"LogIns": [{
"Username": "Mike",
"password": "123"
}, {
"Username": "Farrah",
"password": "123"
}, {
"Username": "John",
"password": "123"
}, {
"Username": "John",
"password": "doe123"
}]
}
另请参阅使用[]=
代替代替array_push
可以节省使用函数的开销。
您的下一个问题:
从json对象中删除对象只需将初始json字符串解码为一个数组并对该数组进行操作,即可使用unset
,
ex删除项。unset($array['LogIns'][2])
将使用键2删除元素
覆盖文件
file_put_contents
已经做到了,如果您确实想附加到文件,则需要传递FILE_APPEND
标志
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句