Yêu cầu Jan 30th, 2019 1:56 p.m. 1039 0 2
  • 1039 0 2
0

Xử lý json trong php

Chia sẻ
  • 1039 0 2

Chào mọi người ! Tình hình là mình có đoạn json như sau :

{
   "status":true,
   "message":"Successfully received info.",
   "site":"Youtube",
   "title":"Taylor Swift - Shake It Off", 
   "like_count":6290433,
   "dislike_count":744939,
   "view_count":2234478374,
   "duration":242,
   "upload_date":"20140818",
   "description":"Taylor\u2019s new release 1989 is Available Now featuring the hit single \u201cShake It Off\u201d and her latest single \u201cBlank Space\u201d. ",
   "tags":[
      "Taylor",
      "Swift",
      "Shake",
      "It",
      "Off",
      "Big",
      "Machine",
      "Records"],
   "uploader":"TaylorSwiftVEVO",
   "uploader_url":"http:\/\/www.youtube.com\/user\/TaylorSwiftVEVO",
   "thumbnail":"https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/maxresdefault.jpg",
   "streams":[
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?id=9d[...]",
      "format":"audio only (DASH audio)",
      "format_note":"Audio only, no video",
      "extension":"m4a",
      "video_codec":"none",
      "audio_codec":"mp4a.40.5",
      "filesize":1438229,
      "height":null,
      "width":null
   },
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?key=[...]",
      "format":"1920x1080 (1080p)",
      "format_note":"1080p",
      "extension":"webm",
      "video_codec":"vp9",
      "audio_codec":"none",
      "filesize":68016821,
      "height":1080,
      "width":1920
   },
  {
      ...
   }
]}

Cho mình hỏi là làm thế nào để lấy url trong đoạn json trên bằng php

2 CÂU TRẢ LỜI


Đã trả lời Jan 30th, 2019 2:19 p.m.
Đã được chấp nhận
+2

Bạn thử dùng json_decode để biến đổi nó về dạng object sau đó select phần tử cần lấy ra

Chia sẻ
Jan 31st, 2019 3:35 a.m.

bạn có thể chỉ rõ hơn k?

Avatar Vũ Nguyễn @vunguyen10111995
Jan 31st, 2019 4:26 a.m.

@sgorki sau khi bạn dùng hàm json_decode, dữ liệu của bạn sẽ thành

array:15 [
  "status" => true
  "message" => "Successfully received info."
  "site" => "Youtube"
  "title" => "Taylor Swift - Shake It Off"
  "like_count" => 6290433
  "dislike_count" => 744939
  "view_count" => 2234478374
  "duration" => 242
  "upload_date" => "20140818"
  "description" => "Taylor’s new release 1989 is Available Now featuring the hit single “Shake It Off” and her latest single “Blank Space”. "
  "tags" => array:8 [
    0 => "Taylor"
    1 => "Swift"
    2 => "Shake"
    3 => "It"
    4 => "Off"
    5 => "Big"
    6 => "Machine"
    7 => "Records"
  ]
  "uploader" => "TaylorSwiftVEVO"
  "uploader_url" => "http://www.youtube.com/user/TaylorSwiftVEVO"
  "thumbnail" => "https://i.ytimg.com/vi/nfWlot6h_JM/maxresdefault.jpg"
  "streams" => array:2 [
    0 => array:9 [
      "url" => "https://r6---sn-4g57knkz.googlevideo.com/videoplayback?id=9d[...]"
      "format" => "audio only (DASH audio)"
      "format_note" => "Audio only, no video"
      "extension" => "m4a"
      "video_codec" => "none"
      "audio_codec" => "mp4a.40.5"
      "filesize" => 1438229
      "height" => null
      "width" => null
    ]
    1 => array:9 [
      "url" => "https://r6---sn-4g57knkz.googlevideo.com/videoplayback?key=[...]"
      "format" => "1920x1080 (1080p)"
      "format_note" => "1080p"
      "extension" => "webm"
      "video_codec" => "vp9"
      "audio_codec" => "none"
      "filesize" => 68016821
      "height" => 1080
      "width" => 1920
    ]
  ]
]

Giờ bạn muốn lấy url trong cái đoạn trên thì bạn chỉ việc làm như sau

$url = [];
foreach ($data2['streams'] as $stream) {
    $url[] = $stream['url'];
};

return $url;

Vậy là ra được 2 url như bạn mong muốn mà

Avatar Vũ Nguyễn @vunguyen10111995
Jan 31st, 2019 6:41 a.m.

bạn đã xử lý được chưa vậy

Jan 31st, 2019 10:19 a.m.

@vunguyen10111995 tra ve ket qua nhu nay thi lam kieu j : http://sgorki.000webhostapp.com/vid.php

Avatar Lê Vĩnh Thiện @le.vinh.thien
Feb 4th, 2019 3:53 a.m.

@sgorki cũng tương tự thôi, dùng json_decode rồi lấy ra giống kiểu bạn lấy giá trị của 1 phần tử trong mảng. 👍

Đã trả lời Jan 30th, 2019 2:34 p.m.
+4
<?php
        //Enter your code here, enjoy!

$data = '{
   "status":true,
   "message":"Successfully received info.",
   "site":"Youtube",
   "title":"Taylor Swift - Shake It Off", 
   "like_count":6290433,
   "dislike_count":744939,
   "view_count":2234478374,
   "duration":242,
   "upload_date":"20140818",
   "description":"Taylor\u2019s new release 1989 is Available Now featuring the hit single \u201cShake It Off\u201d and her latest single \u201cBlank Space\u201d. ",
   "tags":[
      "Taylor",
      "Swift",
      "Shake",
      "It",
      "Off",
      "Big",
      "Machine",
      "Records"],
   "uploader":"TaylorSwiftVEVO",
   "uploader_url":"http:\/\/www.youtube.com\/user\/TaylorSwiftVEVO",
   "thumbnail":"https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/maxresdefault.jpg",
   "streams":[
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?id=9d[...]",
      "format":"audio only (DASH audio)",
      "format_note":"Audio only, no video",
      "extension":"m4a",
      "video_codec":"none",
      "audio_codec":"mp4a.40.5",
      "filesize":1438229,
      "height":null,
      "width":null
   },
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?key=[...]",
      "format":"1920x1080 (1080p)",
      "format_note":"1080p",
      "extension":"webm",
      "video_codec":"vp9",
      "audio_codec":"none",
      "filesize":68016821,
      "height":1080,
      "width":1920
   }
]}';

$data2 = json_decode($data, true);


foreach ($data2['streams'] as $value) {
  echo $value['url'];
  echo "\n";
}

Kết quả trả về:

https://r6---sn-4g57knkz.googlevideo.com/videoplayback?id=9d[...]

https://r6---sn-4g57knkz.googlevideo.com/videoplayback?key=[...]

Chia sẻ
Jan 31st, 2019 3:34 a.m.

chỉ lấy cái url thứ 2 thì thế nào?

Avatar Dao Thai Son @dao.thai.son
Jan 31st, 2019 3:49 a.m.
foreach ($data2['streams'] as $key=> $value) {
  if ($key == 1) {
    echo $value['url'];
     echo "\n";
 } 
}
Jan 31st, 2019 3:58 a.m.

@dao.thai.son kết quả nó trả về kiểu này thì lấy kiểu gì nhỉ ? http://sgorki.000webhostapp.com/vid.php

Avatar Dao Thai Son @dao.thai.son
Jan 31st, 2019 4:09 a.m.

@sgorki json_decode object đó về thành array rồi access vào từng element của mảng.

Jan 31st, 2019 10:20 a.m.

@dao.thai.son ban co the code mau gium dc khong nhi?

Jan 31st, 2019 12:00 p.m.

@sgorki Bạn có thể tìm hiểu và code mà, dân dev là phải thế mới hay chứ. Mình không biết 1 chút gì về php mình search cú pháp của nó mình trả lời cho bạn như trên đó. Mình nghĩ việc tìm hiểu mấy cái này nhanh mà, người khác code cho được 1 lần đâu có code cho được mãi. Xin lỗi nếu bạn không thích phần trả lời này của mình.

Jan 31st, 2019 2:01 p.m.

@QuangPH minh k tim thay bai huong dan

Jan 31st, 2019 2:16 p.m.

@sgorki Mình search google 3 lần:

  • php online editor
  • string to json php
  • for loop in php
Feb 1st, 2019 3:03 a.m.

@QuangPH cảm ơn bạn

Feb 12th, 2019 2:52 a.m.

json_decode(strJson, true); nó sẽ trả về arrData rồi xử lý bình thường thoai

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí