PHP 解析 ElasticSearch 的 json 方法,有遍所有 json 元素。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
s884812
V2EX    问与答

PHP 解析 ElasticSearch 的 json 方法,有遍所有 json 元素。

  •  1
     
  •   s884812 2014-12-18 17:35:01 +08:00 3334 次点击
    这是一个创建于 3970 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家好。
    小弟在工作遇到一困。
    以下是eleasticsearch返回的json料:
    {
    "took" : 12,
    "timed_out" : false,
    "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
    },
    "hits" : {
    "total" : 8,
    "max_score" : 2.6739764,
    "hits" : [ {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "6",
    "_score" : 2.6739764,
    "_source":{
    "user": "dean",
    "version": "0",
    "device_vendor": "security",
    "device_product": "threatmanager",
    "device_version": "1.0",
    "signature_id": "100",
    "description": "worm successfully stopped",
    "severity": "10",
    "extension": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    }
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "5",
    "_score" : 2.3862944,
    "_source":{
    "user": "dean",
    "version": "0",
    "device_vendor": "security",
    "device_product": "threatmanager",
    "device_version": "1.0",
    "signature_id": "100",
    "description": "worm successfully stopped",
    "severity": "10",
    "extension": "src=10.0.0.1 dst=2.1.2.2 spt=1232",
    "ext1": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    }
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "AUpMu6M4z71lXPfoDG1F",
    "_score" : 2.098612,
    "_source":{"user":"dean","version":"0","device_vendor":"security","device_product":"threatmanager","device_version": "1.0","signature_id":"100","description":"worm successfully stopped","severity":"10","extension":"src=10.0.0.1 dst=2.1.2.2 spt=1333","ext4": "src=10.0.0.1 dst=2.1.2.2 spt=1232","ext6": "src=10.0.0.1 dst=2.1.2.2 spt=1232"}
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "AUpMxKDDz71lXPfoDG1G",
    "_score" : 2.098612,
    "_source":{"user":"dean","version":"0","device_vendor":"security","device_product":"threatmanager","device_version": "1.0","signature_id":"100","description":"worm successfully stopped","severity":"10","extension":"src=10.0.0.1 dst=2.1.2.2 spt=1333","ext2": "src=10.0.0.1 dst=2.1.2.2 spt=1232"}
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "4",
    "_score" : 2.098612,
    "_source":{
    "user": "dean",
    "version": "0",
    "device_vendor": "security",
    "device_product": "threatmanager",
    "device_version": "1.0",
    "signature_id": "100",
    "description": "worm successfully stopped",
    "severity": "10",
    "extension": "src=10.0.0.1 dst=2.1.2.2 spt=1232",
    "ext62": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    }
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "3",
    "_score" : 2.098612,
    "_source":{
    "user": "dean",
    "version": "0",
    "device_vendor": "security",
    "device_product": "threatmanager",
    "device_version": "1.0",
    "signature_id": "100",
    "description": "worm successfully stopped",
    "severity": "10",
    "extension": "src=10.0.0.1 dst=2.1.2.2 spt=1232",
    "ext10": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    }
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "2",
    "_score" : 1.5108256,
    "_source":{
    "user": "dean",
    "version": "0",
    "device_vendor": "security",
    "device_product": "threatmanager",
    "device_version": "1.0",
    "signature_id": "100",
    "description": "worm successfully stopped",
    "severity": "10",
    "extension": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    "ext7": "src=10.0.0.1 dst=2.1.2.2 spt=1232"
    }
    }, {
    "_index" : "cef",
    "_type" : "alert",
    "_id" : "AUpMuF-Pz71lXPfoDG1E",
    "_score" : 1.5108256,
    "_source":{"user":"dean","version":"0","device_vendor":"security","device_product":"threatmanager","device_version": "1.0","signature_id":"100","description":"worm successfully stopped","severity":"10","extension":"src=10.0.0.1 dst=2.1.2.2 spt=1232","ext19": "src=10.0.0.1 dst=2.1.2.2 spt=1232","ext41": "src=10.0.0.1 dst=2.1.2.2 spt=1232","ext9": "src=10.0.0.1 dst=2.1.2.2 spt=1232"}
    } ]
    }
    }

    各位可以看到,在Extension後方有不定量的ext位(上不只ext),有有三,有有一,甚至十。
    目前我解析的方式是
    decoded = json_decode($json); //decode json
    $results = $decoded->hits->hits;
    foreach ($results as $item) {
    $id = $item->_id; //get the id
    $version = $item->_source->version; // get the version
    $user = $item->_source->user; // get the user
    $device_vendor = $item->_source->deviceVendor; // get the device_vendor
    $device_product = $item->_source->deviceProduct; // get the device_product
    $device_version = $item->_source->deviceVersion; // get the device_version
    $signature_id = $item->_source->signatureId; // get the signature_id
    $description = $item->_source->name; // get the description
    $severity = $item->_source->severity; // get the severity
    $extension = $item->_source->extension; // get the extension

    }

    子的法可以清楚的去出我需求的料,前提是我知道回的位是什。
    那像在法位的情形,只能去遍整json,但我不知道如何下手,希望各位先指一下。
    感!
    2 条回复    2014-12-18 21:11:07 +08:00
    kslr
        1
    kslr  
       2014-12-18 18:09:56 +08:00
    json_decode 的第二个参数是设置是否返回数组
    设置后直接获得父数组然后遍历
    s884812
        2
    s884812  
    OP
       2014-12-18 21:11:07 +08:00
    @kslr 有有的作法呢?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1121 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:00 PVG 02:00 LAX 11:00 JFK 14:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86