gpt4 book ai didi

javascript - 从 JSON 对象获取字符串

转载 作者:行者123 更新时间:2023-12-02 16:58:43 24 4
gpt4 key购买 nike

我有一个名为 $graphData 的 JSON 对象,当我使用 <?php echo var_dump(json_decode($graphData)); ?> 时我得到以下信息:

object(stdClass)[987]
public 'myself' =>
object(stdClass)[984]
public '1' =>
object(stdClass)[986]
public 'id' => string '999999999' (length=9)
public 'value' => string '4.2' (length=3)
public 'name' => string 'Myself' (length=6)
public 'owner' => string '' (length=0)
public 'type' => int 1
public 'children' =>
array (size=0)
...
public 'my_teams' =>
array (size=0)
empty
public 'my_units' =>
array (size=0)
empty
public 'companies' =>
array (size=1)
0 =>
object(stdClass)[982]
public 'id' => string '66' (length=2)
public 'name' => string 'Company' (length=8)
public 'owner' => string 'Name Name' (length=13)
public 'value' => string '4.2' (length=3)
public 'type' => string '4' (length=1)
public 'children' =>
array (size=0)
...

如何访问标记为“value”且值为 4.2 的字符串?

谢谢

//编辑:我需要在php或js代码中使用它

最佳答案

在 PHP 中:

$data = json_decode($graphData);
$value = $data->companies[0]->value;
//Or for the one stored under "myself"
$value = $data->myself->{'1'}->value;

在 JavaScript 中:

var value = data.companies[0].value;
//Or for the one stored under "myself"
value = data.myself[1].value;

关于javascript - 从 JSON 对象获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com