3

这种类型的数组的正确名称是什么?

有 3 个主要部分和 4 个子部分,由“issuedTime”“text”“url”和“validToTime”组成,如何开始将其转换为对象?如果只有 1 个主要部分,这将是相当简单的,但是有 3 个主要部分并且没有每个主要部分的标识让我挠头不知道从哪里开始。

任何建议表示赞赏。

[{
"issuedTime":"7:13pm Sunday 13 June 2010",
"text":"\nAmended 7:10pm.\n\nText text and more text\n",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00am Monday 14 June 2010"
},{
"issuedTime":"8:33pm Sunday 13 June 2010",
"text":"\nText and more text.\n",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00pm Monday 14 June 2010"
},{
"issuedTime":"10:40am Sunday 13 June 2010",
"text":"\nAnd even more text.",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00am Tuesday 15 June 2010"
}
]
4

4 回答 4

10

JSON(JavaScript Object Notation 的首字母缩写词)是一种轻量级的基于文本的开放标准,专为人类可读的数据交换而设计。它源自 JavaScript 编程语言,用于表示简单的数据结构和关联数组,称为对象。尽管它与 JavaScript 有关系,但它是独立于语言的,几乎所有编程语言都有解析器。

您可以轻松地将其转换为带有函数的 php 数组json_decode,以下是 php 站点的示例:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));

结果:

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

to的第二个参数json_decode是是否应该转换为关联数组。如果不指定第二个参数,输出将如下所示:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

因此,您可以将其转换为数组并循环遍历它,如下所示:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$array = json_decode($json, true);
print_r($array);

foreach($array as $key => $value)
{
   // manipulate the var $value
}
于 2010-06-13T09:44:11.883 回答
2

这是解析该json的方法

<?php
$json = '[{
    "issuedTime":"7:13pm Sunday 13 June 2010",
    "text":"\nAmended 7:10pm.\n\nText text and more text\n",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00am Monday 14 June 2010"
    },{
    "issuedTime":"8:33pm Sunday 13 June 2010",
    "text":"\nText and more text.\n",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00pm Monday 14 June 2010"
    },{
    "issuedTime":"10:40am Sunday 13 June 2010",
    "text":"\nAnd even more text.",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00am Tuesday 15 June 2010"
}]';
// Parse the json into a PHP array that holds multiple "stdClass Object"s
$obj = json_decode($json);  
// Iterate through each "stdClass Object" and show what it contains  
foreach($obj as $var => $value)
{
    echo "Number: $var <br/>";    
    echo "Issued: " . $obj[$var]->issuedTime . "<br/>";                    
    echo "Text: " . $obj[$var]->text . "<br/>";    
    echo "URL: " . $obj[$var]->url . "<br/>";    
    echo "Valid to: " . $obj[$var]->validToTime . "<br/>";       
    echo "<br/>";
}
?>

一旦你有了 $obj,你就可以像上面的 foreach 循环一样访问它,或者访问,比如说你将使用的第二个清单中的文本:

echo $obj[1]->text; // Second listing, since the first listing is $obj[0]

和相同的 HTML 链接 w 文本:

echo '<a href="' . $obj[1]->url . '">' . $obj[1]->text . '</a>';
于 2010-06-13T13:13:17.553 回答
1

看看: http ://www.lost-in-code.com/programming/php-code/php-array-to-object/

于 2010-06-13T12:53:11.457 回答
1

你试过json_decode()吗?它应该正确地将其解析为stdObject.

于 2010-06-13T09:45:52.203 回答