Add "name" to a JSON object

Asked

Viewed 1,322 times

4

My JSON currently returns this low code:

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

Is there a way in places of numbers 2, 8 and 13 I could add a "header"? I wanted it to be 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)

Someone can help me, if that’s possible?

  • Click [Edit] and put the code that generates JSON. What you want is very simple, but we need the code to help better (the part that gets the values, not only the encode). In this case, it would be by a name in the objects, not a "header". JSON objects are defined as "nome":valor. http://json.org/ Probably these numbers are coming from some array, and that’s where we have to adjust the names, before the meeting.

  • Okay, I’ve changed it

  • draw the json output you expect, so that we do it here.

  • 1

    I already edited the question, making it more complete, with the commands I used to generate, as it turns out, and as I want to leave...thanks :d

  • Improved a lot with its edition, but repeating the names does not give. Each pair needs a unique name. If you want the same name, the format will look different. To create things with the same name, it will look like an array of pairs: [{"nome":"maria"},{"nome":"pedro"},{"nome":"zuul"}], otherwise it is not a valid JSON.

  • you want to change the numeric keys to text, got it. I just don’t understand where this variable came from $variavel , would not be the $result?

  • there is no way then?

  • As designed, can not do with JSON. It could generate as a string, but depends a lot on where you will use. If the party that will receive the data wants a JSON, it has to be as I posted in the comment above, an array of objects: [{"nome":"maria"},{"nome":"pedro"},{"nome":"zuul"}]

  • yes, just explain to me what you’re doing... where this variable is coming from $variavel. Your code doesn’t seem to be complete.

  • Alterei la, where it was $variavel put $result

  • I just don’t understand why you want it this way, because it’s not a correct json, but... you can do it.

Show 6 more comments

3 answers

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 this way, because when using JSON, you must have a key for each one, it would be something similar to this:

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

And if so, you should do it this way:

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

echo json_encode($collection);

3


You just need to turn your json in array:

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

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

I don’t know much about php, but you need to create an array this way:

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

more info: http://www.dyn-web.com/tutorials/php-js/json/array.php

  • Yeah, that’s what I’m trying to do, like I would?

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 as many JSON libraries will error when reading a JSON with repeated keys, and even if there is no error, they probably won’t be able to read all the data. Using your example of wanting a result:

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

In doing:

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

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

It is not possible to do with json_encode()

Because json_encode() does not produce objects with repeated keys. You have to do Gambi in 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, take out the keys?

It looks like the numeric keys that are bothering you. If the "header" is a fixed thing, how about nay put it, and yes 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"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.