0

i have a file with inside data formatted in json format:

{
   "data": 

     {

   "users": [


    {

        "ragione_sociale": "",
        "nome": "Annamaria Zaccariello",
        "indirizzo": "",
        "cap": "",
        "citta": "",
        "provincia": "",
        "telefono1": "",
        "telefono2": "",
        "fax": "",
        "cellulare": "",
        "codice_fiscale": "",
        "newsletter": "1",
        "sesso": "",
        "username": "annamaria.zaccariello@libero.it",
        "scadenza_tessera": "09/08/2012",
        "tipo_tessera": "Red Carpet",
        "stato_tessera": "0",
        "user_id": "1",
        "punti": "0"
    },
    {

        "ragione_sociale": "",
        "nome": "Debora Chessa",
        "indirizzo": "",
        "cap": "",
        "citta": "",
        "provincia": "ge",
        "telefono1": "",
        "telefono2": "",
        "fax": "",
        "cellulare": "3494339496",
        "codice_fiscale": "",
        "newsletter": "1",
        "sesso": "F",
        "username": "debora_c85@hotmail.it",
        "scadenza_tessera": "05/10/2012",
        "tipo_tessera": "Membership",
        "stato_tessera": "0",
        "user_id": "968",
        "punti": "0"
    } 
    ]
     }

}

I need to parse all users one by one and extracting for each one some information "nome", "username", "scadenza_tessera", "tipo_tessera", "stato_tessera", "punti", "provincia", "cellulare"

Anyone can paste PHP code to do this operation ?

Thanks

fyr
  • 20,227
  • 7
  • 37
  • 53
  • 2
    Don't you need to hide phone numbers and e-mail addrress of those people befor publication here? Debora and Annamaria might not be happy about that :) – Damien Pirsy Oct 06 '11 at 05:58
  • I have absolutly no idea what i can do ! This is first time i try to parse a JSON file neved did before. I tried to follow some tutorials and finding others response in here but i was unable to find similar eaxample ! – Enrico Baldi Oct 06 '11 at 06:03
  • Check this `Parsing JSON file with PHP` http://stackoverflow.com/questions/4343596/parsing-json-file-with-php – Rami Alshareef Oct 06 '11 at 06:10

1 Answers1

2

If you save your json stuff in $myjson you will be able to do something like this:

$myjson = <<< EOT
{
   "data": 

     {

   "users": [


    {

        "ragione_sociale": "",
        "nome": "Annamaria Zaccariello",
        "indirizzo": "",
        "cap": "",
        "citta": "",
        "provincia": "",
        "telefono1": "",
        "telefono2": "",
        "fax": "",
        "cellulare": "",
        "codice_fiscale": "",
        "newsletter": "1",
        "sesso": "",
        "username": "annamaria.zaccariello@libero.it",
        "scadenza_tessera": "09/08/2012",
        "tipo_tessera": "Red Carpet",
        "stato_tessera": "0",
        "user_id": "1",
        "punti": "0"
    },
    {

        "ragione_sociale": "",
        "nome": "Debora Chessa",
        "indirizzo": "",
        "cap": "",
        "citta": "",
        "provincia": "ge",
        "telefono1": "",
        "telefono2": "",
        "fax": "",
        "cellulare": "3494339496",
        "codice_fiscale": "",
        "newsletter": "1",
        "sesso": "F",
        "username": "debora_c85@hotmail.it",
        "scadenza_tessera": "05/10/2012",
        "tipo_tessera": "Membership",
        "stato_tessera": "0",
        "user_id": "968",
        "punti": "0"
    } 
    ]
     }

}
EOT;

$obj=json_decode($myjson);
foreach($obj->data->users as $user){
        echo $user->username;
}

However if you insert invalid JSON $obj will be null. This is not covered by the snippet.

fyr
  • 20,227
  • 7
  • 37
  • 53