Mostrando entradas con la etiqueta JSON. Mostrar todas las entradas
Mostrando entradas con la etiqueta JSON. Mostrar todas las entradas

domingo, 21 de diciembre de 2014

generar json en php

<?php

$student = array("id"=>101,
"name"=>"John Doe",
"isStudent"=>true,
"scores"=>array(10, 20, 30, 40),
"courses" => array(
"major"=>"Finance",
"minor"=>"Marketing"
)
);

echo json_encode($student); //encoding the array into a JSON string
//and using echo to  print the output

?>

viernes, 27 de septiembre de 2013

usar json con jquery

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
    <title>JavaScript And JSON</title>
</head>
<body>

<h2>Links</h2>
<ol id="links">
</ol>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="myscript.js"></script>

</body>
</html>


myscript.js

$(document).ready(function() {

    $.getJSON('data.json', function(info){

            var output='';
            for (var i = 0; i <= info.links.length-1; i++) {
                for (key in info.links[i]) {
                    if (info.links[i].hasOwnProperty(key)) {
                        output += '<li>' +
                        '<a href = "' + info.links[i][key] +
                        '">' + key + '</a>';
                        '</li>';
                }  
                }
            }
           
            var update = document.getElementById('links');
            update.innerHTML = output;

    }); //getJSON

}); // ready




data.json

{
    "full_name" : "Ray Villalobos",
    "title" : "Staff Author",
    "links" : [
            { "blog"     : "http://iviewsource.com" },
            { "facebook" : "http://facebook.com/iviewsource" },
            { "podcast"  : "http://feeds.feedburner.com/authoredcontent" },
            { "twitter"  : "http://twitter.com/planetoftheweb" },
            { "youtube"  : "http://www.youtube.com/planetoftheweb" }
        ]
} 




usar json con ajax

var request;
if (window.XMLHttpRequest) {
    request=new XMLHttpRequest();
} else {
    request=new ActiveXObject("Microsoft.XMLHTTP");
}

request.open('GET', 'data.json');
request.onreadystatechange = function() {
    if ((request.status === 200) &&
        (request.readyState === 4)) {

            info = JSON.parse(request.responseText);

            var output='';
            for (var i = 0; i <= info.links.length-1; i++) {
                for (key in info.links[i]) {
                    if (info.links[i].hasOwnProperty(key)) {
                        output += '<li>' +
                        '<a href = "' + info.links[i][key] +
                        '">' + key + '</a>';
                        '</li>';
                }  
                }
            }
           
            var update = document.getElementById('links');
            update.innerHTML = output;
           
           
    } //ready
} //event
request.send();

Recorrer un objeto json

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
    <title>JavaScript And JSON</title>
</head>
<body>
<h2>Links</h2>
<ul id="links">
</ul>


<script>

var info = {
"full_name" : "Ray Villalobos",
"title" : "Staff Author",
"links" : {
    "blog"     : "http://iviewsource.com",
    "facebook" : "http://facebook.com/iviewsource",
        "youtube"  : "http://www.youtube.com/planetoftheweb",
        "podcast"  : "http://feeds.feedburner.com/authoredcontent",
        "twitter"  : "http://twitter.com/planetoftheweb"
    }
};

var output = "";

for ( key in info.links ) {
    if (info.links.hasOwnProperty(key)) {
        output += '<li>' +
        '<a href = "' + info.links[key] +
        '">' + key + '</a>' +
        '</li>';
    } //if the links has the key property
} // for...go through each link

var update = document.getElementById('links');
update.innerHTML = output;


</script>
</body>
</html>