Add "name" to a JSON object

4

My JSON currently returns this low code:

{"2":"aluno","8":"barbara_cristina","13":"carolina_deus"}

How do I add a "header" in numbers 2, 8 and 13? I wanted it to look like this:

{"nome":"aluno","nome":"barbara_cristina","nome":"carolina_deus"}

These are the commands I use to generate JSON.

$aluno = array();    
while($row = mysqli_fetch_array($verifica))
{ 
   $aluno[]= $row['username'];
} 

$alunoToledo = array(); 
while($lista2 = mysqli_fetch_array($verifica2))
{  
   $alunoToledo[] = $lista2['username'];
}

$result = array_diff($aluno,$alunoToledo);
json_encode($result)

Can anyone help me if this is possible?

    
asked by anonymous 14.04.2016 / 18:40

3 answers

3

You just need to transform your json to array :

Converts this: {"nome":"aluno","nome":"barbara_cristina","nome":"carolina_deus"}

In this: [{"nome":"aluno"},{"nome":"barbara_cristina"},{"nome":"carolina_deus"}]

I do not understand much of php, but you need to create an array like this:

$alunos= array( array( 'nome' => $aluno ), array( 'nome'....

More info: link

    
14.04.2016 / 18:59
5

To do this, simply add the keys:

$aluno = array();    
while($row = mysqli_fetch_array($verifica))
{ 
$aluno[]= $row['username'];
} 

$alunoToledo = array(); 
while($lista2 = mysqli_fetch_array($verifica2))
{  
$alunoToledo[] = $lista2['username'];
}

$results = array_diff($aluno,$alunoToledo);
echo '{nome:' . $results[0] . ',nome:' . $results[1] . ',nome:' . $results[2] . '}';

But the use of JSON, is not valid in this way, because when using JSON, you must have a key for each one, it would look something like this:

[{nome: "nome1"},{nome: "nome2"},{nome: "nome3"},{nome: "nome4"}]

And so, you should do it this way:

$collection = array();
foreach ($results as $result){
    $collection[]['nome'] = $result;  
}

echo json_encode($collection);
    
14.04.2016 / 19:05
1

It's possible, but it's weird

What you are trying to do is a JSON object with repeated keys / properties. This is unusual because many JSON libraries will fail to read a JSON with repeated keys, and even if it does not give an error, they will probably not allow you to read all the data. Using your example of wanting a result:

  

{"name": "student", "name": "barbara_cristina", "name": "carolina_deus"}

When doing:

$obj = json_decode( $json );
echo $obj->nome;

You expect me to print what? aluno , barbara_cristina or carolina_deus ?

Unable to do with json_encode()

Because json_encode() does not produce objects with repeated keys. You have to make gambi in your hand. Something like this:

<?php
$result = array( "2" => "aluno" , "8" => "barbara_cristina" , "13" => "carolina_deus" );

$fakeItens = array();
foreach ( $result as $item )
    $fakeItens[] = '"nome":' . json_encode( $item );
$fakeJson = "{" . implode( ',' , $fakeItens ) . "}";
echo $fakeJson;

This code will generate correct JSON, even if the item has quotes or special characters.

Instead of putting a fixed header, remove the keys?

It looks like the numeric keys that are bothering you. If the "header" is a fixed thing, how do you not put it, but take the numbers?

<?php
$result = array( "2" => "aluno" , "8" => "barbara_cristina" , "13" => "carolina_deus" );
echo json_encode( array_values( $result ) );

Compare the result of the two codes:

{"nome":"aluno","nome":"barbara_cristina","nome":"carolina_deus"}
["aluno","barbara_cristina","carolina_deus"]
    
14.04.2016 / 19:35