【问题标题】:Retrieve Json Decode Elements检索 Json 解码元素
【发布时间】:2013-07-25 01:54:13
【问题描述】:

我尝试了很多方法,但仍然无法从下面的输出中返回用户名。我该怎么做?我在下面运行了这一行并得到了下面的字符串:

$work = exec($cmd);
var_dump($work);
$json_object = json_decode($work, true);




string(1118) "{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry@aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T21:50:58-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}" "

回声:

{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry@aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T22:11:09-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}

var_dump:

string(1118) "{"user"=>{"id"=>4151878, "username"=>"jerry_smithjerry", "firstname"=>"Jerry ", "lastname"=>"Smith", "birthday"=>nil, "sex"=>0, "city"=>nil, "state"=>nil, "country"=>nil, "registration_date"=>"2013-07-24T16:12:20-04:00", "about"=>"", "domain"=>"jerry_smithjerry.500px.com", "fotomoto_on"=>false, "locale"=>"en", "show_nude"=>false, "fullname"=>"Jerry Smith", "userpic_url"=>"/graphics/userpic.png", "upgrade_status"=>0, "store_on"=>false, "email"=>"jerry_smithjerry@aol.com", "upload_limit"=>20, "upload_limit_expiry"=>"2013-07-24T22:11:09-04:00", "upgrade_type"=>0, "upgrade_status_expiry"=>nil, "auth"=>{"facebook"=>0, "twitter"=>0}, "contacts"=>{}, "equipment"=>{}, "photos_count"=>0, "affection"=>0, "in_favorites_count"=>0, "friends_count"=>0, "followers_count"=>0, "not_active"=>true, "avatars"=>{"default"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "large"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "small"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}, "tiny"=>{"http"=>"/graphics/userpic.png", "https"=>"/graphics/userpic.png"}}}}"

【问题讨论】:

  • 试试var_dump($work)
  • @DevZer0 我只想将用户名的值存储为变量。
  • 我明白,但是你的 json_decode 返回一个字符串,所以你可以在 json_decode 之前运行var_dump($work) 并显示输出
  • @user2600095 echo 或 var_dump $work 的值。奇怪的是它返回一个字符串。
  • @kennypu,当我回显时,它不返回字符串。当我 var_dump 时,它返回一个字符串。

标签: php json


【解决方案1】:

您的 json 字符串没有有效的 json,json 字符串使用 : 作为键值分隔符,在您的情况下,您使用了像 => 表示法这样的 php 关联数组。这个json不是有效的json,

【讨论】:

  • 好的,那我该怎么办?这可能就是我没有让它工作的原因。
  • 这不是答案。如果你尝试他给出的字符串,它将返回 NULL 而不是字符串。
  • @kennypu 我很困惑。你说的是运行什么命令?
  • @user2600095 看看我的回答。编辑您的帖子以显示您设置 $work 的位置。
【解决方案2】:

据此:json_decode returns string type instead of object 您可能在 json 字符串上运行 json_encode()。如果要将对象或数组转换为 json,则使用 json_encode()

如果您在该代码之前的某处执行$work = json_encode($json_string) 之类的操作,那就是原因。删除该行,在原始 json 字符串上运行 json_decode() 应该没问题。

一旦你把它充实起来,你就可以做到

$json_object = json_decode($your_json_string); 
echo $json_object->user->username; //echo's the username

【讨论】:

  • @DevZer0 啊,我想知道这是怎么回事?这应该是不可能的,因为 json_decode 返回一个对象、数组或 null 如果它不是有效的 json。
  • @kennypu 这就是为什么我一直告诉 OP 显示 $work 的 var_dump
  • @DevZer0 我假设我编辑的答案是它返回字符串的原因。
猜你喜欢
  • 2021-12-26
  • 2022-01-19
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
相关资源
最近更新 更多