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有安全隐患! 现在大多数 ...
随机推荐
- 《Remus: High Availability via Asychronous Virtual Machine Replication》翻译
Abstract 想要让应用能够躲过硬件故障是一项非常昂贵的任务,因为这通常意味着对软件进行重构,使它包含复杂的恢复逻辑的同时需要部署专用的硬件,而这些对于提升大型的或者遗留的应用的可靠性是巨大的障碍 ...
- 嵌入式Linux驱动学习之路(九)Linux系统调用、驱动程序框架
应用程序通过open read write close 等函数来操作计算机硬件.类似是一个接口. 当应用程序调用这些接口程序时,计算机是如何进入内核的呢?这是经过了系统调用. 实际上当调用接口函数 ...
- u-boot移植初步尝试-tiny4412
获取u-boot源代码 在u-boot官方网站下载uboot源码.ftp://ftp.denx.de/pub/u-boot/ 因为是第一次移植uboot,所以这里选的版本是 u-boot-2013.0 ...
- AndroidStudio 混淆打包
AndroidStudio中的项目可以用compile的形式引入github上的开 源项目,可以引用module,而不一定都要用libs文件夹中添加jar包的形式.在最终realease打包时,混淆的 ...
- 适配ipone5
PROJECT和TARGETS都需要设置
- 4种sql分页
四种方式实现SQLServer 分页查询 SQLServer 的数据分页: 假设现在有这样的一张表:CREATE TABLE test( id int primary key not null ide ...
- 如何撰写PRD
PRD(Product-Requirement-Document,产品需求文档),这对于任何一个产品经理来说都不会陌生的一个文档,一个PRD是衡量一个产品经理整体思维的标准,一个PRD可以看出一个产品 ...
- ES6 数组解构赋值
.数组解构 let [a, b, c,d] = ["aa", "bb", 77,88]; alert(a) //弹出aa 可以用babel 解析看ES5的转换结 ...
- Linux 网络编程详解八
TCP/IP协议三次握手机制 TCP/IP是全双工通道,两端都可以读写,三次握手机制就是验证TCP/IP是否是全双工通道 1.客户端调用connect()函数,阻塞客户端进程,客户端向服务器发送数据包 ...
- SQL Server数据库代码指令简介
这些是比较常用的命令操作,事先声明,这些命令是不区分大小写的,我按照我的课本来总结用法和知识点,无用的章节自动省略. 没有一点数据库知识基础的可以等我录制视频,不然可能看不懂,视频链接:http:// ...