1

I am trying to parse json data from online database with a php file link and i dont know how to do it. Generally i know that parsing json data is something like this.

<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';

    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong

    alert(json.address.streetAddress); //88 8nd Street
    alert(json["address"].city); //New York

    alert(json.phoneNumber[0].number); //111 111-1111
    alert(json.phoneNumber[1].type); //fax

    alert(json.phoneNumber.number); //undefined
</script>   

But what if i create a json file with php and it has the link

www.example.com/parse.php

and i want that link to connect it with another html file in another server like

www.example2.com/parsehere.html

which has the above code without putting the json data inline, but to load it from the link of php?? Is that possible?

All im trying to do is to make inside this script to load data from a url and NOT from raw json data like the above script.

<script>
         //here something to load data from a link and parse it below.

        var json = JSON.parse(data);

        alert(json["name"]); //mkyong
        alert(json.name); //mkyong

        alert(json.address.streetAddress); //88 8nd Street
        alert(json["address"].city); //New York

        alert(json.phoneNumber[0].number); //111 111-1111
        alert(json.phoneNumber[1].type); //fax

        alert(json.phoneNumber.number); //undefined
    </script>   
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74

1 Answers1

1

If I am reading your question right you want a php-script to generate json? Below is a part of a php-script I made that returns json.This is save as a file.php and called through the web.

$return_arr = array();

while($row = mysql_fetch_assoc($dbresult)) {
    if($row["firstletter"] != '')
    {   

        $row_array['id'] = $row['id'];
        $row_array['firstletter'] = $row['firstletter'];
        $row_array['languageid'] = $row['languageid'];
        $row_array['word'] = $row['word'];
        $row_array['description'] = $row['description'];
        $row_array['imageList'] = $row['imageList'];
         array_push($return_arr,$row_array);

    }
}

header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
$json = json_encode($return_arr);
echo $json;

To fetch your php-json use this in your code:

var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() { 
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }

        anHttpRequest.open( "GET", aUrl, true );            
        anHttpRequest.send( null );
    }
}

Then to make the actual call use this:

var myClient = new HttpClient();
myClient.get('http://yourphp.php', function(response) {
    // Do what you want with the response
});
Joakim M
  • 1,793
  • 2
  • 14
  • 29