PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素
以下是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,但我不知道該如何下手,希望各位先進指點一下。
感謝!
PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素的更多相关文章
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- .NET中常用的几种解析JSON方法
一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...
- JSON格式自动解析遇到的调用方法问题.fromJson() ..readValue()
所使用的API Store是 聚合数据 使用 手机归属地查询 功能 因百度的apistore.baidu.com 2016年12月开始至今天不接受新用户调取.聚合数据一个接口免费. 一.通过谷歌的go ...
- 【转】IOS中Json解析的四种方法
原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有 ...
- scala解析json —— json4s 解析json方法汇总
使用json4s的框架,包括spark,flink 1.org.json4s 引入pom的方法 对于本地支持,引入以下依赖项添加到pom中 <dependency> <groupId ...
- 【转】C# 解析JSON方法总结
http://blog.csdn.net/jjhua/article/details/51438317 主要参考http://blog.csdn.NET/joyhen/article/details/ ...
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
先看一下我要实现的功能界面: 这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面 ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- 字符串转化为json方法
1.function strToJson(str){ var json = eval('(' + str + ')'); return json; } 不过eval解析json有安全隐患! 现在大多数 ...
随机推荐
- fiddler抓包工具1
名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FTP等 Host 请求地址的主机名 U ...
- Machine Learning Algorithms Study Notes(2)--Supervised Learning
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...
- 【转】【收藏】LINQ学习心得分享--------(二)LINQ语法详解
原地址:http://blog.csdn.net/xuemoyao/article/details/8053444 通过上一章节的学习,相信大家已经掌握了学习LINQ的前期的准备知识.在这一节里, ...
- 64位win7硬盘安装64位ubuntu 13.04
最近本来是准备通过升级的方式把ubuntu从12.04升级到12.10再升级到13.04的,但是升级到12.10之后,可能是因为某一步的操作不当,出现无法进入系统的情况.不过还好的是升级之前保存了主要 ...
- Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package 1.0.1 supports: net (.NETFramework,Version=v0.0)
Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package ...
- Java8 Lambda表达式和流操作如何让你的代码变慢5倍
原文出处:ImportNew 有许许多多关于 Java 8 中流效率的讨论,但根据 Alex Zhitnitsky 的测试结果显示:坚持使用传统的 Java 编程风格——iterator 和 for- ...
- 树莓派及其他硬件平台国内外Linux镜像站全汇总
转载至segmentfault. 本文收集整理国内对于Raspberry Pi.CubieBoard.pcDuino等各种Linux开源硬件平台,所使用的Raspbian.Arch Linux ARM ...
- Discuz! X2.5判断会员登录状态及外部调用注册登录框
Discuz! X2.5判断会员登录状态及外部调用注册登录框 有关discuz论坛会员信息,收集的一些资料: 用dedecms+discuz做了个门户加论坛形式的网站,但是dedecms顶部目前只能q ...
- SuperSlidev2.1滑动门
1.引用jQuery.js 和 jquery.SuperSlide.js 因为SuperSlide是基于jQuery的插件,所以前提必须先引用jQuery,再引用SuperSlide <head ...
- 使用bootstrap-table简化CRUD
1. 引入bootstrap-table资源包, 页首引用css, 页脚引用js 2. table 参数说明 data-toggle="table" data-toolbar=&q ...