解析此JSON文件结构 [英] Parsing this JSON file structure
问题描述
我有一个文件,内部数据的格式为json格式:
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"
}
]
}
}
我需要一个一个地解析所有用户,并为每个用户提取一些信息"nome",用户名","scadenza_tessera","tipo_tessera","stato_tessera","punti","provincia","cellulare"
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"
任何人都可以粘贴PHP代码来执行此操作吗?
Anyone can paste PHP code to do this operation ?
谢谢
推荐答案
如果将json内容保存在$myjson
中,您将可以执行以下操作:
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;
}
但是,如果您插入无效的JSON,则$obj
将是null
.该代码段未涵盖此内容.
However if you insert invalid JSON $obj
will be null
. This is not covered by the snippet.
这篇关于解析此JSON文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!