Requirement : how can i convert the following json format.
From :
[
{"u0":{"user_id":"124", "name":"Eloise R. Morton"}},
{"u1":{"user_id":"126", "name":"Mary S. Williams"}}
]
To :
{
"u0":{"user_id":"124", "name":"Eloise R. Morton"},
"u1":{"user_id":"126", "name":"Mary S. Williams"}
}
I want to delete/remove the first and last square brackets from JSON.
PHP code :
$sql = "SELECT * FROM user_list";
$result = $conn->query($sql);
$user_count=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_count_id="u".$user_count;
$user_count++;
$user_list[]= array($user_count_id=>$row);
}
} else {
echo "0 results";
}
echo json_encode($user_list);
$conn->close();
Javascript code :
function update_user_list(){
//Fetch data
$.ajax( "user_list.php" ).done(function(json) {
user_list = json;
var add_user="";
for(var obj in user_list){
add_user += '...generate HTML.....';
}
$('.user-list-panel .user-table').html(add_user);
})
.fail(function() {
alert( "error" );
});
You are adding them yourself when you add values to your array in php:
$user_list[] = array($user_count_id=>$row);
If you don't want that, you should use:
$user_list[$user_count_id] = $row;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With