douji8549 2017-02-14 03:11
浏览 12
已采纳

PHP解码JSON得到uname? [重复]

This question already has an answer here:

How do I get the uname value from this JSON into PHP variable? to use through my page?

    {
        "token": "iutiutiut-0jjjjj0-97987g",
        "auth": {
            "id": 1,
            "app_id": 1,
            "user": {
                "uname": "foo",  
                "role": "member"
            }
    }
}

thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.

could you also tell me the correct terminology so I know as well thanks

</div>
  • 写回答

1条回答 默认 最新

  • dua55014 2017-02-14 03:20
    关注

    What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:

    $obj = json_decode($json_string);
    $uname = $obj->auth->user->uname;
    

    Or, if you prefer the array syntax, use the second argument in json_decode:

    $arr = json_decode($json_string, true);
    $uname = $obj['auth']['user']['uname'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?