如何在PHP中打印JSON数据 [英] How to print JSON data in PHP

查看:943
本文介绍了如何在PHP中打印JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下JSON结果中打印这些键名称","adminName1","countryCode".

I want to print these keys "name","adminName1","countryCode" from following JSON result.

{
"geonames": [{
    "adminCode1": "16",
    "lng": "74.19774",
    "distance": "2.95838",
    "geonameId": 1254611,
    "toponymName": "Thengode",
    "countryId": "1269750",
    "fcl": "P",
    "population": 0,
    "countryCode": "IN",
    "name": "Thengode",
    "fclName": "city, village,...",
    "countryName": "India",
    "fcodeName": "populated place",
    "adminName1": "Maharashtra",
    "lat": "20.51997",
    "fcode": "PPL"
}]
}

如何正确处理?

推荐答案

只需使用json_decode并获取值.由于您的json_decode函数返回一个对象数组,因此您需要使用->来访问值.

Just use json_decode and get the values. Because your json_decode function returns an object array you need to use -> for accessing the values.

如果您的json字符串名称为$json.

If your json string name were $json.

$arr = json_decode($json);

echo $arr->geonames[0]->name; //Thengode
echo $arr->geonames[0]->adminName1; //Maharashtra
echo $arr->geonames[0]->countryCode; //IN

您的json解码数组为:

You json decoded array would be:

stdClass Object
(
    [geonames] => Array
        (
            [0] => stdClass Object
                (
                    [adminCode1] => 16
                    [lng] => 74.19774
                    [distance] => 2.95838
                    [geonameId] => 1254611
                    [toponymName] => Thengode
                    [countryId] => 1269750
                    [fcl] => P
                    [population] => 0
                    [countryCode] => IN
                    [name] => Thengode
                    [fclName] => city, village,...
                    [countryName] => India
                    [fcodeName] => populated place
                    [adminName1] => Maharashtra
                    [lat] => 20.51997
                    [fcode] => PPL
                )

        )

)

这篇关于如何在PHP中打印JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆