简体   繁体   English

用PHP解析嵌套的JSON

[英]Parsing nested JSON with PHP

{
    "listing": {
        "@attributes": {
            "domain": "example.com"
        },
        "tld": "com",
        "sld": "example",
        "owner": "John Smith"
    }
}

I am needing to iterate through this JSON array and put the values into PHP variables so that I can return the values. 我需要遍历此JSON数组并将值放入PHP变量中,以便可以返回值。

Example: 例:

echo $sld;

would print: 将打印:

example

Would I need to do this with a foreach loop (and if so how would I format this) or is there a easy built in function such as extract() that will do this? 我需要使用foreach循环来执行此操作(如果可以的话,该如何格式化该格式)?或者是否存在一个简单的内置函数(例如extract()来执行此操作?

<?php 
$json = '{
    "listing": {
        "@attributes": {
            "domain": "example.com"
        },
        "tld": "com",
        "sld": "example",
        "owner": "John Smith"
    }
}';

$decoded_array = json_decode($json, true);
echo $decoded_array['listing']['sld'];//example
?> 

You can use 您可以使用

json_decode('your json string', true);

Which will return an associative array of your string which you can then loop though. 这将返回您的字符串的关联数组,然后可以循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM