dtmm0148603 2011-10-06 05:55
浏览 48
已采纳

解析此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"
    } 
    ]
     }

}

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

  • 写回答

1条回答 默认 最新

  • dongshuzhuo5659 2011-10-06 06:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题