-1

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

$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"}}}}"
4

2 回答 2

0

Your json string doesn't have valid json, the json strings uses : as a key value separator, in your case you have used a php associative array like => notation. This json is not valid json,

于 2013-07-25T02:16:41.647 回答
-1

根据这个:json_decode 返回字符串类型而不是对象您可能正在运行json_encode()json 字符串。json_encode()如果要将对象或数组转换为 json,则使用。

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

一旦你充实了这一点,你就可以做到

$json_object = json_decode($your_json_string); 
echo $json_object->user->username; //echo's the username
于 2013-07-25T01:58:52.470 回答