6

I am beginner in programming, I want someone to give me an example to show me how to write JSON data with HTML text

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    </head>
    <body>
        <h1>antar<h2>
        <script>
            $.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
                document.open();    
                document.write('Latitude: ' + data.latitude + '\nLongitude: ' + data.longitude + '\nCountry: ' + data.address.country);
                document.close();
            });
        </script>
    </body>
</html>
2
  • 1
    do you want the JSON data to be printed in the page? Commented Dec 31, 2012 at 10:20
  • <?php echo json_encode(array('test'=>'text1','test2'=>'text2'))?>...this will print the array in json.. Commented Dec 31, 2012 at 10:21

2 Answers 2

5

You can add in your html something like:

 <h2 id='lat'></h2>
    <h2 id='long'></h2>
    <h2 id='country'></h2>

and then refactor your script as:

$(document).ready(function(){

$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {

  $("#lat").text(data.latitude);  
  $("#long").text(data.longitude);  
  $("#country").text(data.country);      

});
 });

​Here a working example you can play with.

Sign up to request clarification or add additional context in comments.

7 Comments

Given the OP's previous question I'm pretty sure this is the answer he's looking for.
@user1939335 It's just an idea/example: jQuery is a DOM manipulation library, you can even use other strategy and leverage jQuery too, dig in some doc examples to see how.
is all the same what about this json for example api.wunderground.com/api/102376e7c0e1c995/geolookup/conditions/…
the last example is just more complex, since you have array and you probably want to iterate them: se jQuery.each() function. You probably want to add them in a table
<script src="ajax.googleapis.com/ajax/libs/jquery/1.5.1/…> <script> jQuery(document).ready(function($) { $.ajax({ url : "api.wunderground.com/api/102376e7c0e1c995/geolookup/conditions/…", dataType : "jsonp", success : function(parsed_json) { var location = parsed_json['location']['city']; var temp_f = parsed_json['current_observation']['temp_f']; alert("Current temperature in " + location + " is: " + temp_f); } }); }); </script> that's how to read it ......
|
0

use JSON.stringify and inject it on any DOM element example with a div with ID mydiv

var a = JSON.stringify(myJSON);

document.getElementById('mydiv').innerHTML(a);

Reference:

https://github.com/douglascrockford/JSON-js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.