如何从此json响应中获取一个确切值?

时间:2019-10-03 11:36:13

标签: php json

我有以下JSON响应。我想从中获取文本值。请让我知道如何执行此操作。 如何获取'text'字段的"Киргизия, Бишкек, Чуйский проспект, 213"字段的值?

{ 
   "response":{ 
      "GeoObjectCollection":{ 
         "metaDataProperty":{ 
            "GeocoderResponseMetaData":{ 
               "Point":{ 
                  "pos":"74.596998 42.87681"
               },
               "request":"74.596998,42.876810",
               "results":"10",
               "found":"7"
            }
         },
         "featureMember":[ 
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"exact",
                        "text":"Киргизия, Бишкек, Чуйский проспект, 213",
                        "kind":"house",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, Бишкек, Чуйский проспект, 213",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"город республиканского подчинения Бишкек"
                              },
                              { 
                                 "kind":"locality",
                                 "name":"Бишкек"
                              },
                              { 
                                 "kind":"street",
                                 "name":"Чуйский проспект"
                              },
                              { 
                                 "kind":"house",
                                 "name":"213"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, Бишкек, Чуйский проспект, 213",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"город республиканского подчинения Бишкек",
                                 "Locality":{ 
                                    "LocalityName":"Бишкек",
                                    "Thoroughfare":{ 
                                       "ThoroughfareName":"Чуйский проспект",
                                       "Premise":{ 
                                          "PremiseNumber":"213"
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  },
                  "name":"Чуйский проспект, 213",
                  "description":"Бишкек, Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"74.592804 42.873796",
                        "upperCorner":"74.601015 42.879834"
                     }
                  },
                  "Point":{ 
                     "pos":"74.59691 42.876815"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"street",
                        "text":"Киргизия, Бишкек, Чуйский проспект",
                        "kind":"street",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, Бишкек, Чуйский проспект",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"город республиканского подчинения Бишкек"
                              },
                              { 
                                 "kind":"locality",
                                 "name":"Бишкек"
                              },
                              { 
                                 "kind":"street",
                                 "name":"Чуйский проспект"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, Бишкек, Чуйский проспект",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"город республиканского подчинения Бишкек",
                                 "Locality":{ 
                                    "LocalityName":"Бишкек",
                                    "Thoroughfare":{ 
                                       "ThoroughfareName":"Чуйский проспект"
                                    }
                                 }
                              }
                           }
                        }
                     }
                  },
                  "name":"Чуйский проспект",
                  "description":"Бишкек, Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"74.562262 42.875005",
                        "upperCorner":"74.637046 42.877918"
                     }
                  },
                  "Point":{ 
                     "pos":"74.599641 42.876069"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"other",
                        "text":"Киргизия, Бишкек, Первомайский район",
                        "kind":"district",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, Бишкек, Первомайский район",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"город республиканского подчинения Бишкек"
                              },
                              { 
                                 "kind":"locality",
                                 "name":"Бишкек"
                              },
                              { 
                                 "kind":"district",
                                 "name":"Первомайский район"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, Бишкек, Первомайский район",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"город республиканского подчинения Бишкек",
                                 "Locality":{ 
                                    "LocalityName":"Бишкек",
                                    "DependentLocality":{ 
                                       "DependentLocalityName":"Первомайский район"
                                    }
                                 }
                              }
                           }
                        }
                     }
                  },
                  "name":"Первомайский район",
                  "description":"Бишкек, Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"74.50971 42.842926",
                        "upperCorner":"74.611777 42.972625"
                     }
                  },
                  "Point":{ 
                     "pos":"74.573553 42.892537"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"other",
                        "text":"Киргизия, город республиканского подчинения Бишкек",
                        "kind":"province",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, город республиканского подчинения Бишкек",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"город республиканского подчинения Бишкек"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, город республиканского подчинения Бишкек",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"город республиканского подчинения Бишкек"
                              }
                           }
                        }
                     }
                  },
                  "name":"город республиканского подчинения Бишкек",
                  "description":"Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"74.464992 42.786526",
                        "upperCorner":"74.718964 42.972731"
                     }
                  },
                  "Point":{ 
                     "pos":"74.58454 42.859034"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"other",
                        "text":"Киргизия, Бишкек",
                        "kind":"locality",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, Бишкек",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"город республиканского подчинения Бишкек"
                              },
                              { 
                                 "kind":"locality",
                                 "name":"Бишкек"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, Бишкек",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"город республиканского подчинения Бишкек",
                                 "Locality":{ 
                                    "LocalityName":"Бишкек"
                                 }
                              }
                           }
                        }
                     }
                  },
                  "name":"Бишкек",
                  "description":"Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"74.464992 42.786526",
                        "upperCorner":"74.718964 42.972731"
                     }
                  },
                  "Point":{ 
                     "pos":"74.60371 42.876366"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"other",
                        "text":"Киргизия, Чуйская область",
                        "kind":"province",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия, Чуйская область",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              },
                              { 
                                 "kind":"province",
                                 "name":"Чуйская область"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия, Чуйская область",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия",
                              "AdministrativeArea":{ 
                                 "AdministrativeAreaName":"Чуйская область"
                              }
                           }
                        }
                     }
                  },
                  "name":"Чуйская область",
                  "description":"Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"73.046166 41.842738",
                        "upperCorner":"77.226809 43.266764"
                     }
                  },
                  "Point":{ 
                     "pos":"74.522457 42.602641"
                  }
               }
            },
            { 
               "GeoObject":{ 
                  "metaDataProperty":{ 
                     "GeocoderMetaData":{ 
                        "precision":"other",
                        "text":"Киргизия",
                        "kind":"country",
                        "Address":{ 
                           "country_code":"KG",
                           "formatted":"Киргизия",
                           "Components":[ 
                              { 
                                 "kind":"country",
                                 "name":"Киргизия"
                              }
                           ]
                        },
                        "AddressDetails":{ 
                           "Country":{ 
                              "AddressLine":"Киргизия",
                              "CountryNameCode":"KG",
                              "CountryName":"Киргизия"
                           }
                        }
                     }
                  },
                  "name":"Киргизия",
                  "boundedBy":{ 
                     "Envelope":{ 
                        "lowerCorner":"69.276601 39.171874",
                        "upperCorner":"80.283174 43.266764"
                     }
                  },
                  "Point":{ 
                     "pos":"74.670572 41.892642"
                  }
               }
            }
         ]
      }
   }
}

1 个答案:

答案 0 :(得分:0)

所以我做了一个小脚本,试图明确显示我在做什么:

$json = '{"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"Point":{"pos":"74.596998 42.87681"},"request":"74.596998,42.876810","results":"10","found":"7"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"exact","text":"Киргизия, Бишкек, Чуйский проспект, 213","kind":"house","Address":{"country_code":"KG","formatted":"Киргизия, Бишкек, Чуйский проспект, 213","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"город республиканского подчинения Бишкек"},{"kind":"locality","name":"Бишкек"},{"kind":"street","name":"Чуйский проспект"},{"kind":"house","name":"213"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, Бишкек, Чуйский проспект, 213","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"город республиканского подчинения Бишкек","Locality":{"LocalityName":"Бишкек","Thoroughfare":{"ThoroughfareName":"Чуйский проспект","Premise":{"PremiseNumber":"213"}}}}}}}},"name":"Чуйский проспект, 213","description":"Бишкек, Киргизия","boundedBy":{"Envelope":{"lowerCorner":"74.592804 42.873796","upperCorner":"74.601015 42.879834"}},"Point":{"pos":"74.59691 42.876815"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"street","text":"Киргизия, Бишкек, Чуйский проспект","kind":"street","Address":{"country_code":"KG","formatted":"Киргизия, Бишкек, Чуйский проспект","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"город республиканского подчинения Бишкек"},{"kind":"locality","name":"Бишкек"},{"kind":"street","name":"Чуйский проспект"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, Бишкек, Чуйский проспект","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"город республиканского подчинения Бишкек","Locality":{"LocalityName":"Бишкек","Thoroughfare":{"ThoroughfareName":"Чуйский проспект"}}}}}}},"name":"Чуйский проспект","description":"Бишкек, Киргизия","boundedBy":{"Envelope":{"lowerCorner":"74.562262 42.875005","upperCorner":"74.637046 42.877918"}},"Point":{"pos":"74.599641 42.876069"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"other","text":"Киргизия, Бишкек, Первомайский район","kind":"district","Address":{"country_code":"KG","formatted":"Киргизия, Бишкек, Первомайский район","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"город республиканского подчинения Бишкек"},{"kind":"locality","name":"Бишкек"},{"kind":"district","name":"Первомайский район"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, Бишкек, Первомайский район","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"город республиканского подчинения Бишкек","Locality":{"LocalityName":"Бишкек","DependentLocality":{"DependentLocalityName":"Первомайский район"}}}}}}},"name":"Первомайский район","description":"Бишкек, Киргизия","boundedBy":{"Envelope":{"lowerCorner":"74.50971 42.842926","upperCorner":"74.611777 42.972625"}},"Point":{"pos":"74.573553 42.892537"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"other","text":"Киргизия, город республиканского подчинения Бишкек","kind":"province","Address":{"country_code":"KG","formatted":"Киргизия, город республиканского подчинения Бишкек","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"город республиканского подчинения Бишкек"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, город республиканского подчинения Бишкек","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"город республиканского подчинения Бишкек"}}}}},"name":"город республиканского подчинения Бишкек","description":"Киргизия","boundedBy":{"Envelope":{"lowerCorner":"74.464992 42.786526","upperCorner":"74.718964 42.972731"}},"Point":{"pos":"74.58454 42.859034"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"other","text":"Киргизия, Бишкек","kind":"locality","Address":{"country_code":"KG","formatted":"Киргизия, Бишкек","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"город республиканского подчинения Бишкек"},{"kind":"locality","name":"Бишкек"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, Бишкек","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"город республиканского подчинения Бишкек","Locality":{"LocalityName":"Бишкек"}}}}}},"name":"Бишкек","description":"Киргизия","boundedBy":{"Envelope":{"lowerCorner":"74.464992 42.786526","upperCorner":"74.718964 42.972731"}},"Point":{"pos":"74.60371 42.876366"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"other","text":"Киргизия, Чуйская область","kind":"province","Address":{"country_code":"KG","formatted":"Киргизия, Чуйская область","Components":[{"kind":"country","name":"Киргизия"},{"kind":"province","name":"Чуйская область"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия, Чуйская область","CountryNameCode":"KG","CountryName":"Киргизия","AdministrativeArea":{"AdministrativeAreaName":"Чуйская область"}}}}},"name":"Чуйская область","description":"Киргизия","boundedBy":{"Envelope":{"lowerCorner":"73.046166 41.842738","upperCorner":"77.226809 43.266764"}},"Point":{"pos":"74.522457 42.602641"}}},{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"precision":"other","text":"Киргизия","kind":"country","Address":{"country_code":"KG","formatted":"Киргизия","Components":[{"kind":"country","name":"Киргизия"}]},"AddressDetails":{"Country":{"AddressLine":"Киргизия","CountryNameCode":"KG","CountryName":"Киргизия"}}}},"name":"Киргизия","boundedBy":{"Envelope":{"lowerCorner":"69.276601 39.171874","upperCorner":"80.283174 43.266764"}},"Point":{"pos":"74.670572 41.892642"}}}]}}}';



$jsonArray = json_decode($json);





$response = $jsonArray->response;
$geoObjectCollection = $response->GeoObjectCollection;
$featureMember = $geoObjectCollection->featureMember;

foreach($featureMember as $item){

        if(property_exists($item,'GeoObject')){
            $geoObject = $item->GeoObject;
            if(property_exists($geoObject,'metaDataProperty')){
                $metaDataProperty = $geoObject->metaDataProperty;
                if(property_exists($metaDataProperty,'GeocoderMetaData')){
                    $GeocoderMetaData = $metaDataProperty->GeocoderMetaData;
                    if(property_exists($GeocoderMetaData,'text')){
                        $text = $GeocoderMetaData->text;
                        echo '<pre>';
                        var_dump($text);
                        echo '<pre>';
                    }
                }
            }

        }

PHPTESTER上尝试过