博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素
阅读量:7016 次
发布时间:2019-06-28

本文共 4334 字,大约阅读时间需要 14 分钟。

以下是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,但我不知道該如何下手,希望各位先進指點一下。
感謝!

转载于:https://www.cnblogs.com/zhangchenliang/p/4206467.html

你可能感兴趣的文章
哈希表的静态,动态,以及key/value形式
查看>>
rpm命令如何打印调试信息?
查看>>
C#语言学习--基础部分(二) 方法重载
查看>>
HDU_1143_tri tiling
查看>>
codeforces_1075_C. The Tower is Going Home
查看>>
C# 关于XML的简单操作实例
查看>>
ggplot2:画世界地图和中国地图 合并数据 增添信息 标记
查看>>
VertexBuffer渲染次序
查看>>
python中使用 xpath
查看>>
集中管理:领导者,不能不考虑的几件事之—— 拿什么辅助你,我的决策?(一)...
查看>>
关于VirtualBox虚拟机安装GhostXP出现蓝屏proce***.sys 的解决办法
查看>>
JSP如何在servlet将一个数据模型对象传递给jsp页面
查看>>
PHP 实现“贴吧神兽”验证码
查看>>
根据一个表的数据情况显示另一个表的数据
查看>>
TP4056大电流1A使用注意事项
查看>>
Java常考面试题(四)
查看>>
学习Javascript闭包(Closure)
查看>>
你性格那么软,总是经常改变想法
查看>>
NeHe OpenGL教程 第十七课:2D图像文字
查看>>
学习SpringMVC——从HelloWorld开始
查看>>