【发布时间】:2020-09-12 19:23:07
【问题描述】:
您好,我有一个 json 文件,我想从中提取一些数据 该文件如下所示:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 4.0201406,
"hits" : [
{
"_index" : "catalogue",
"_type" : "_doc",
"_id" : "p95iEXIBfVAlCluoT3sT",
"_score" : 4.0201406,
"_source" : {
"categorie" : "Bonbons ",
"marque" : "TIC TAC",
"nom" : "bonbon duo TIC TAC"
}
},
{
"_index" : "catalogue",
"_type" : "_doc",
"_id" : "od5iEXIBfVAlCluoT3sT",
"_score" : 3.6080353,
"_source" : {
"categorie" : "Bonbons",
"marque" : "TIC TAC",
"nom" : "Bonbons duo de fraises TIC TAC"
}
}
]
}
}
这是 elasticserch 查询的结果,我的问题是如何使用 php 从该文件中检索类别、商标、标称及其值? 我尝试将此结果转换为数组,我使用了 json_decode() 但我总是得到 null 然后我使用 Service_json() 并解决了问题。 在那之后,我得到了这个结果,但现在我很难得到一些特定的数据,比如类别、标称、品牌和它们的值,我得到了空结果。 这是我的 php 代码:
array(1) {
["hits"]=>
object(stdClass)#3 (1) {
["hits"]=>
array(7) {
[0]=>
object(stdClass)#4 (2) {
["_id"]=>
string(20) "kN5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#5 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(29) "Bonbons parfums fruits MENTOS"
}
}
[1]=>
object(stdClass)#6 (2) {
["_id"]=>
string(20) "kd5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#7 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(21) "Bonbons menthe MENTOS"
}
}
[2]=>
object(stdClass)#8 (2) {
["_id"]=>
string(20) "kt5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#9 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(37) "Bonbons caramel/chocolat blanc MENTOS"
}
}
[3]=>
object(stdClass)#10 (2) {
["_id"]=>
string(20) "k95iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#11 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "Mentos"
["nom"]=>
string(31) "Bonbons caramel/chocolat MENTOS"
}
}
[4]=>
object(stdClass)#12 (2) {
["_id"]=>
string(20) "lN5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#13 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(28) "Bonbons menthe sucres MENTOS"
}
}
[5]=>
object(stdClass)#14 (2) {
["_id"]=>
string(20) "ld5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#15 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(31) "Bonbons framboise orange MENTOS"
}
}
[6]=>
object(stdClass)#16 (2) {
["_id"]=>
string(20) "lt5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#17 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(26) "Bonbons pomme verte MENTOS"
}
}
}
}
}
【问题讨论】:
-
你想从 JSON 中准确提取什么数据?
标签: javascript php jquery json elasticsearch