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" );
});
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" );
});
Share
Improve this question
edited Apr 11, 2016 at 13:56
Rohìt Jíndal
27.2k15 gold badges77 silver badges132 bronze badges
asked Apr 11, 2016 at 13:33
aasim bairagdaraasim bairagdar
3011 gold badge4 silver badges10 bronze badges
1
-
$.getJSON('user_list.php', function(json){
or$.ajax({ dataType: "json", url: "user_list.php", data: data, success: success });
– Alexey G Commented Apr 11, 2016 at 13:39
3 Answers
Reset to default 5You 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;
You can do it by updating your PHP code.
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[$user_count_id]=$row;
}
}
// Above script will create single dimensional array with unique keys.
You have to slightly modify your code in order to get the designed structure:
$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[$user_count_id] = $row;
}
} else {
echo "0 results";
}
echo json_encode($user_list);
$conn->close();
See the line: $user_list[$user_count_id] = $row;