Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] duda con uso de json

Estas en el tema de duda con uso de json en el foro de PHP en Foros del Web. hola a todos es que anto tratando de hacer esto mismo @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código XML: Ver original <channel > <title > Roku-Developer-Sample-Feed </title > <category ...
  #1 (permalink)  
Antiguo 11/05/2017, 12:27
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 10 meses
Puntos: 10
duda con uso de json

hola a todos es que anto tratando de hacer esto mismo
Código XML:
Ver original
  1. <channel>
  2. <title>Roku-Developer-Sample-Feed</title>
  3. <category name="Action">
  4. <item>
  5. <title>Live Gaming</title>
  6. <seriesId>10001</seriesId>
  7. <contentType>Series</contentType>
  8. <link>
  9. http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/decbe34b64ea4ca281dc09997d0f23fd/aac0cfc54ae74fdfbb3ba9a2ef4c7080/117_segment_2_twitch__nw_060515.mp4
  10. </link>
  11. <description>
  12. With the Twitch channel, you can watch the most popular broadcasts of the day, browse live broadcasts by the games you love and follow your favorite Twitch broadcasters.
  13. </description>
  14. <pubDate>Thu, 11 Jun 2015 16:51:07 GMT</pubDate>
  15. <guid isPermaLink="false">decbe34b64ea4ca281dc09997d0f23fd</guid>
  16. <media:content channels="2" bitrate="1328.0" duration="53" fileSize="8731706" framerate="23.976" height="720" type="video/mp4" width="1280" isDefault="true" url="http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/decbe34b64ea4ca281dc09997d0f23fd/aac0cfc54ae74fdfbb3ba9a2ef4c7080/117_segment_2_twitch__nw_060515.mp4">
  17. <media:description>
  18. With the Twitch channel, you can watch the most popular broadcasts of the day, browse live broadcasts by the games you love and follow your favorite Twitch broadcasters.
  19. </media:description>
  20. <media:keywords>episode 21, roku recommends, twitch</media:keywords>
  21. <media:thumbnail url="http://s2.content.video.llnw.net/lovs/images-prod/59021fabe3b645968e382ac726cd6c7b/media/decbe34b64ea4ca281dc09997d0f23fd/j5_.540x304.jpeg"/>
  22. <media:title>Live Gaming</media:title>
  23. </media:content>
  24. </item>
  25. <item>
  26. <title>Let's Get Cookin'</title>
  27. <seriesId>10002</seriesId>
  28. <contentType>Series</contentType>
  29. <link>
  30. http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/6c9d0951d6d74229afe4adf972b278dd/bc227724e2854c6b93ad48d1feb39c16/roku_ep_115_segment_5_paula_nw_050515.mp4
  31. </link>
  32. <description>
  33. The Paula Deen Channel is your instant access to Paula Deens new and hilarious cooking and lifestyle shows. Paula and her sons, Jamie and Bobby, will cook up their delicious southern recipes and help you with hints and tips that make meal planning simple, affordable and easy.
  34. </description>
  35. <pubDate>Thu, 21 May 2015 18:47:20 GMT</pubDate>
  36. <guid isPermaLink="false">6c9d0951d6d74229afe4adf972b278dd</guid>
  37. <media:content channels="2" bitrate="1328.0" duration="59" fileSize="9523612" framerate="23.976" height="720" type="video/mp4" width="1280" isDefault="true" url="http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/6c9d0951d6d74229afe4adf972b278dd/bc227724e2854c6b93ad48d1feb39c16/roku_ep_115_segment_5_paula_nw_050515.mp4">
  38. <media:description>
  39. The Paula Deen Channel is your instant access to Paula Deens new and hilarious cooking and lifestyle shows. Paula and her sons, Jamie and Bobby, will cook up their delicious southern recipes and help you with hints and tips that make meal planning simple, affordable and easy.
  40. </media:description>
  41. <media:keywords>episode 18, paula deen, roku recommends</media:keywords>
  42. <media:thumbnail url="http://s2.content.video.llnw.net/lovs/images-prod/59021fabe3b645968e382ac726cd6c7b/media/6c9d0951d6d74229afe4adf972b278dd/JME.540x304.jpeg"/>
  43. <media:title>Let's Get Cookin'</media:title>
  44. </media:content>
  45. </item>
  46. </category>
  47. <category name="Comedy">
  48. <item>
  49. <title>Live Gaming</title>
  50. <seriesId>10003</seriesId>
  51. <contentType>Series</contentType>
  52. <link>
  53. http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/decbe34b64ea4ca281dc09997d0f23fd/aac0cfc54ae74fdfbb3ba9a2ef4c7080/117_segment_2_twitch__nw_060515.mp4
  54. </link>
  55. <description>
  56. With the Twitch channel, you can watch the most popular broadcasts of the day, browse live broadcasts by the games you love and follow your favorite Twitch broadcasters.
  57. </description>
  58. <pubDate>Thu, 11 Jun 2015 16:51:07 GMT</pubDate>
  59. <guid isPermaLink="false">decbe34b64ea4ca281dc09997d0f23fd</guid>
  60. <media:content channels="2" bitrate="1328.0" duration="53" fileSize="8731706" framerate="23.976" height="720" type="video/mp4" width="1280" isDefault="true" url="http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/decbe34b64ea4ca281dc09997d0f23fd/aac0cfc54ae74fdfbb3ba9a2ef4c7080/117_segment_2_twitch__nw_060515.mp4">
  61. <media:description>
  62. With the Twitch channel, you can watch the most popular broadcasts of the day, browse live broadcasts by the games you love and follow your favorite Twitch broadcasters.
  63. </media:description>
  64. <media:keywords>episode 21, roku recommends, twitch</media:keywords>
  65. <media:thumbnail url="http://s2.content.video.llnw.net/lovs/images-prod/59021fabe3b645968e382ac726cd6c7b/media/decbe34b64ea4ca281dc09997d0f23fd/j5_.540x304.jpeg"/>
  66. <media:title>Live Gaming</media:title>
  67. </media:content>
  68. </item>
  69. <item>
  70. <title>Let's Get Cookin'</title>
  71. <seriesId>10004</seriesId>
  72. <contentType>Series</contentType>
  73. <link>
  74. http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/6c9d0951d6d74229afe4adf972b278dd/bc227724e2854c6b93ad48d1feb39c16/roku_ep_115_segment_5_paula_nw_050515.mp4
  75. </link>
  76. <description>
  77. The Paula Deen Channel is your instant access to Paula Deens new and hilarious cooking and lifestyle shows. Paula and her sons, Jamie and Bobby, will cook up their delicious southern recipes and help you with hints and tips that make meal planning simple, affordable and easy.
  78. </description>
  79. <pubDate>Thu, 21 May 2015 18:47:20 GMT</pubDate>
  80. <guid isPermaLink="false">6c9d0951d6d74229afe4adf972b278dd</guid>
  81. <media:content channels="2" bitrate="1328.0" duration="59" fileSize="9523612" framerate="23.976" height="720" type="video/mp4" width="1280" isDefault="true" url="http://roku.cpl.delvenetworks.com/media/59021fabe3b645968e382ac726cd6c7b/6c9d0951d6d74229afe4adf972b278dd/bc227724e2854c6b93ad48d1feb39c16/roku_ep_115_segment_5_paula_nw_050515.mp4">
  82. <media:description>
  83. The Paula Deen Channel is your instant access to Paula Deens new and hilarious cooking and lifestyle shows. Paula and her sons, Jamie and Bobby, will cook up their delicious southern recipes and help you with hints and tips that make meal planning simple, affordable and easy.
  84. </media:description>
  85. <media:keywords>episode 18, paula deen, roku recommends</media:keywords>
  86. <media:thumbnail url="http://s2.content.video.llnw.net/lovs/images-prod/59021fabe3b645968e382ac726cd6c7b/media/6c9d0951d6d74229afe4adf972b278dd/JME.540x304.jpeg"/>
  87. <media:title>Let's Get Cookin'</media:title>
  88. </media:content>
  89. </item>
  90. </category>
  91. </channel>

pero quiero hacerlo para json como puedo hacer esto posible
  #2 (permalink)  
Antiguo 11/05/2017, 15:38
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: duda con uso de json

No entiendo esto que tiene que ver con php, puedes explica mejor lo que quieres ?
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 11/05/2017, 16:58
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 10 meses
Puntos: 10
Respuesta: duda con uso de json

Quiero cambiar de xml a json

codigo php para xml
Código PHP:
Ver original
  1. header("Content-type: text/xml");
  2. require_once('config.php');
  3.  
  4. $q_con = mysql_query("SELECT * FROM cat_categorias WHERE cve_cat_tipo_contenido='1' ORDER BY orden ASC");
  5.  
  6. $salida_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
  7. $salida_xml = "<rss xmlns:media=\"http://search.yahoo.com/mrss/\" version=\"2.0\">\n";
  8. $salida_xml .= "<channel>\n";
  9.  
  10. $array = array();
  11.  
  12. while ($res = mysql_fetch_array($q_con)) {
  13.  
  14. $salida_xml .= "<category cve='".$res['cve']."' name='".utf8_encode($res['categoria'])."'>\n";
  15.        
  16. $sql = mysql_query("SELECT
  17.         co.cve,
  18.         co.titulo,
  19.         co.titulo_latino,
  20.         co.poster,
  21.         co.fondo,
  22.         co.anio,
  23.         co.clasificacion,
  24.         co.duracion,
  25.         co.director,
  26.         co.reparto,
  27.         co.cve_cat_categoria,
  28.         co.cve_cat_audio,
  29.         co.pedir_pin,
  30.         co.cve_cat_tipo_contenido as tipo,
  31.         cc.calidad,
  32.         cau.audio,
  33.              cfv.formato_video,
  34.              uv.url_video
  35.     FROM
  36.         contenido co,
  37.         cat_calidad cc,
  38.         cat_audio cau,
  39.        cat_formato_video cfv,
  40.        url_video uv
  41.     WHERE
  42.        FIND_IN_SET( '".$res["cve"]."', co.cve_cat_categoria )
  43.         AND cfv.cve=uv.cve_cat_formato_video
  44.        AND uv.cve_cat_tipo_contenido=co.cve_cat_tipo_contenido
  45.        AND uv.cve_contenido=co.cve
  46.         AND co.cve_cat_tipo_contenido=1
  47.         AND cc.cve=co.cve_cat_calidad
  48.         AND cau.cve=co.cve_cat_audio
  49.     ORDER BY
  50.         co.cve ASC");
  51.  
  52. while($res = mysql_fetch_array($sql)){
  53.      
  54. $salida_xml .= "<item>\n";
  55.  
  56. $sub = mysql_fetch_array( mysql_query("SELECT subtitulo FROM subtitulos WHERE cve_contenido='".$res['cve']."' AND cve_cat_tipo_contenido='".$res['tipo']."'") );
  57.  
  58. $description = mysql_fetch_array(mysql_query("SELECT * FROM sinopsis WHERE cve_contenido='".$res['cve']."' AND cve_cat_tipo_contenido='".$res['tipo']."'"));
  59.  
  60. $audio = mysql_fetch_array(mysql_query("SELECT audio FROM cat_audio WHERE cve='".$res['cve_cat_audio']."'"));
  61.  
  62. $categorias = mysql_query("SELECT cve,categoria FROM cat_categorias");
  63.    
  64. $arrayCategorias = array();
  65. while ($row = mysql_fetch_array($categorias)) {
  66.     $arrayCategorias[$row['cve']] = $row['categoria'];
  67. }
  68.  
  69. /// generos
  70. $separado = explode(",", $res['cve_cat_categoria']);
  71.  
  72. $list = "";
  73. foreach($separado as $gen){
  74.  
  75. $list .= $arrayCategorias[$gen].', ';
  76.  
  77. }
  78.  
  79. $generos = substr($list, 0, -2);
  80.  
  81.  
  82. if (empty($res['poster'])) {
  83.     # code...
  84.     $poster = "no-poster.png";
  85. }else{
  86.     $poster = $res['poster'];
  87. }
  #4 (permalink)  
Antiguo 11/05/2017, 17:01
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 10 meses
Puntos: 10
Respuesta: duda con uso de json

Código PHP:
Ver original
  1. $salida_xml .= "<MoviesId>".$res['cve']."</MoviesId>";
  2. $salida_xml .= "<Title>".utf8_encode($res['titulo_latino'])."</Title>";
  3. $salida_xml .= "<ContentType>Movies</ContentType>";
  4. $salida_xml .= "<Description>".utf8_encode($description['sinopsis'])."</Description>";  
  5. $salida_xml .= "<ReleaseDate>".$res['anio']."</ReleaseDate>";
  6. $salida_xml .= "<Rating>".$res['clasificacion']."</Rating>";
  7. $salida_xml .= "<StarRating>80</StarRating>";
  8. $salida_xml .= "<Actors>".$res['director']."</Actors>";
  9. $salida_xml .= "<Directors>".$res['reparto']."</Directors>";
  10. $salida_xml .= "<Categories>".$generos."</Categories>";
  11. $salida_xml .= "<Length>".$res['duracion']."</Length>";
  12. $salida_xml .= "<StreamUrls>".$res['url_video']."</StreamUrls>";
  13. $salida_xml .= "<StreamFormat>".strtolower($res['formato_video'])."</StreamFormat>";
  14. $salida_xml .= "<HDPosterURL>http://144.217.94.67/posters/".$poster."</HDPosterURL>";
  15. $salida_xml .= "<hdbackgroundimageurl>".$res['fondo']."</hdbackgroundimageurl>";
  16. $salida_xml .= "<Pin>".$res['pedir_pin']."</Pin>";
  17. $salida_xml .= "<SubtitleUrl>".$sub['subtitulo']."</SubtitleUrl>";
  18.  
  19. $salida_xml .= "</item>\n";  
  20.  
  21. }
  22.  
  23.  
  24. $salida_xml .= "</category>\n";
  25.  
  26. }
  27.  
  28. $salida_xml .= "</channel>";
  29. $salida_xml .= "</rss>";
  30. echo $salida_xml;
  #5 (permalink)  
Antiguo 11/05/2017, 21:46
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: duda con uso de json

Lo único que tienes que hacer es crear una estructura que sea la que requieras y convertirla a json ya una parte te la proporciona la consulta lo demás lo haces usando la función json_encode.
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #6 (permalink)  
Antiguo 13/05/2017, 08:17
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 10 meses
Puntos: 10
Respuesta: duda con uso de json

Ok lo que pasa es que tengo 2 consulta para hacer
tengo una consulta de categorias para obtener todas las categorias de la tabla y luego dentro de esa categoria quiero meter todas las movie que trae esa categoria

Ejemplo json:

Código json:
Ver original
  1. {
  2.     "categories": [
  3.         {
  4.             "title":"Categoria 1",
  5.             "items": [
  6.                 {
  7.                     "description": "Welcome to the Hotel Transylvania, Draculas lavish five-stake resort, where monsters and their families can live it up, free to be the monsters they are without humans to bother them. On one special weekend, Dracula(Adam Sandler) has invited all his best friends - Frankenstein and his wife, the Mummy, the Invisible Man, the Werewolf family, and more -  to celebrate his beloved daughter Mavis' 118th birthday.  For Dracula catering to all of these legendary monsters is no problem but the party really starts when one ordinary guy stumbles into the hotel and changes... ",
  8.                     "director": "Director: Genndy Tartakovsky",
  9.                     "id": "0",
  10.                     "image": "assets/largeRowImage1.jpg",
  11.                     "largeRowImage": "https://s3.amazonaws.com/flicdn/roku/flicast/demo_media/largeHotelTrans.jpg",
  12.                     "remotePoster" : "https://s3.amazonaws.com/flicdn/roku/flicast/demo_media/poster_hoteltransylvania.jpg",
  13.                     "movieLength": "| 1h 31m |",
  14.                     "movieLengthShort": "",
  15.                     "movieYear": " 2012 |",
  16.                     "ratingPic": "assets/ratings/rating-pg-wt-fhd.png",
  17.                     "mpaaRating":"pg",
  18.                     "springboardBackground": "assets/show_bg/",
  19.                     "starRating": "assets/stars_5_fhd.png",
  20.                     "stars": 5,
  21.                     "thumbPic": "assets/poster_hoteltransylvania.jpg",
  22.                     "title": "Hotel Transylvania",
  23.                     "trailerURL":"http://flicdn.s3.amazonaws.com/roku/flicast/demo_media/hoteltansylvania-tlr1_h720p_3mbps.mp4",
  24.                     "streamURL":"http://flicdn.s3.amazonaws.com/roku/flicast/demo_media/hoteltansylvania-tlr1_h720p_3mbps.mp4",
  25.                     "duration":153
  26.                 }
  27.             ]
  28.         },
  29.         {
  30.             "title":"Categoria 2",
  31.             "items": [
  32.                 {
  33.                     "description": "Welcome to the Hotel Transylvania, Draculas lavish five-stake resort, where monsters and their families can live it up, free to be the monsters they are without humans to bother them. On one special weekend, Dracula(Adam Sandler) has invited all his best friends - Frankenstein and his wife, the Mummy, the Invisible Man, the Werewolf family, and more -  to celebrate his beloved daughter Mavis' 118th birthday.  For Dracula catering to all of these legendary monsters is no problem but the party really starts when one ordinary guy stumbles into the hotel and changes... ",
  34.                     "director": "Director: Genndy Tartakovsky",
  35.                     "id": "0",
  36.                     "image": "assets/largeRowImage1.jpg",
  37.                     "largeRowImage": "https://s3.amazonaws.com/flicdn/roku/flicast/demo_media/largeHotelTrans.jpg",
  38.                     "remotePoster" : "https://s3.amazonaws.com/flicdn/roku/flicast/demo_media/poster_hoteltransylvania.jpg",
  39.                     "movieLength": "| 1h 31m |",
  40.                     "movieLengthShort": "",
  41.                     "movieYear": " 2012 |",
  42.                     "ratingPic": "assets/ratings/rating-pg-wt-fhd.png",
  43.                     "mpaaRating":"pg",
  44.                     "springboardBackground": "assets/show_bg/",
  45.                     "starRating": "assets/stars_5_fhd.png",
  46.                     "stars": 5,
  47.                     "thumbPic": "assets/poster_hoteltransylvania.jpg",
  48.                     "title": "Hotel Transylvania",
  49.                     "trailerURL":"http://flicdn.s3.amazonaws.com/roku/flicast/demo_media/hoteltansylvania-tlr1_h720p_3mbps.mp4",
  50.                     "streamURL":"http://flicdn.s3.amazonaws.com/roku/flicast/demo_media/hoteltansylvania-tlr1_h720p_3mbps.mp4",
  51.                     "duration":153
  52.                 }
  53.             ]
  54.         },
  55.     ]
  56. }
  #7 (permalink)  
Antiguo 13/05/2017, 09:10
 
Fecha de Ingreso: agosto-2011
Mensajes: 87
Antigüedad: 12 años, 8 meses
Puntos: 3
Respuesta: duda con uso de json

seria un poco mas amistoso trabajar con arrays directamente y luego que estructures el array, pasarlo a JSON...

$array_pelis = json_decode($mi_json)

luego podrias obtener o asignar información

$array_pelis['categories'][3]['title'] = "accion";
$array_pelis['categories'][3]['items'][0]['desciption'] = "El mundode Json en PHP";
$array_pelis['categories'][3]['items'][0]['director'] = "No lo recuerdo el nombre";

que se yo.. y luegos usas json_encode para devolverlo a JSON

YO LO TRBAJARIA ALGO ASI
__________________
No le pare bola a la vida, que la vida no le para bolas a uno!
  #8 (permalink)  
Antiguo 13/05/2017, 09:51
 
Fecha de Ingreso: agosto-2011
Mensajes: 87
Antigüedad: 12 años, 8 meses
Puntos: 3
Respuesta: duda con uso de json

loco no habia leido la vaina de las consultas...

por que no creas el array por consulta y peliculas de una vez?

me explico, obtienes las categorias y haces foreach

Código PHP:

$result_categorias 
=  get_categorias();


foreach (
$result_categorias as $aguja => $valor){

$array_para_json['categories'][$aguja]['title'] = [$valor['nombre_categoria']];

// mediante ID de la categoria consultas todas las peliculas que pertenecen a ella

$resul_peliculas consulta_peliculas_por_id_categoria($valor['id_categoria']);


foreach (
$result_peliculas as $aguja1 => $valor1){

$array_para_json['categories'][$aguja]['items'][$aguja1]['descripcion'] = $valor1['descripcion_pelicula'];
$array_para_json['categories'][$aguja]['items'][$aguja1]['director'] = $valor1['director_pelicula'];
$array_para_json['categories'][$aguja]['items'][$aguja1]['id'] = $valor1['id_pelicula'];

}


seria algo asi, no me esforce ni comprobe el codigo pero es para que tengas una idea

luego solo lo pasas a JSON

}
__________________
No le pare bola a la vida, que la vida no le para bolas a uno!

Última edición por Camatagua123; 13/05/2017 a las 09:58
  #9 (permalink)  
Antiguo 15/05/2017, 10:52
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 10 meses
Puntos: 10
Respuesta: duda con uso de json

gracias a todos pos sus ayuda

Etiquetas: json, url
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:07.