Yêu cầu thg 1 30, 2019 1:56 CH 1036 0 2
  • 1036 0 2
0

Xử lý json trong php

Chia sẻ
  • 1036 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 thg 1 30, 2019 2:19 CH
Đã đượ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ẻ
thg 1 31, 2019 3:35 SA

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

Avatar Vũ Nguyễn @vunguyen10111995
thg 1 31, 2019 4:26 SA

@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
thg 1 31, 2019 6:41 SA

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

thg 1 31, 2019 10:19 SA

@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
thg 2 4, 2019 3:53 SA

@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 thg 1 30, 2019 2:34 CH
+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ẻ
thg 1 31, 2019 3:34 SA

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

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

@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
thg 1 31, 2019 4:09 SA

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

thg 1 31, 2019 10:20 SA

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

thg 1 31, 2019 12:00 CH

@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.

thg 1 31, 2019 2:01 CH

@QuangPH minh k tim thay bai huong dan

thg 1 31, 2019 2:16 CH

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

  • php online editor
  • string to json php
  • for loop in php
thg 2 1, 2019 3:03 SA

@QuangPH cảm ơn bạn

thg 2 12, 2019 2:52 SA

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í