Andre Brandtzæg
Andre Brandtzæg

Reputation: 29

json with request format jsonld

I want to recieve data from a weather API. They say they use json API. But when I look at the Request URL, it's

https://frost.met.no/sources/v0.jsonld?types=SensorSystem

I've tried to use standard js script, but with this url, i get wrong data. I've been struggling for weeks now!

Anyone have a tip for me?

Upvotes: 0

Views: 175

Answers (2)

deralbert
deralbert

Reputation: 1053

What is in your client.responseText when you get the answer from the server? The only difference to "normal" JSON - the tags starting with @symbol. So if your client.responseText says something like this below, then you should also be able to parse JSON-LD (you can find a simple example of parsing here).

I used your access data to get JSON-LD. Then I saved it in the variable myJSONLD and with var myObj = JSON.parse(myJSONLD). Please check this link with working example.

Upvotes: 0

Andre Brandtzæg
Andre Brandtzæg

Reputation: 29

<!DOCTYPE html>
<html>
<body>
    <h2>Create Object from JSON String</h2>
    <div id="ip"></div>
    <div>
        <div id="response">
        </div>
        <p id="demo"></p>
        <input type="button" class="btn btn-primary" value="Call Web API" onclick="javascript:CallWebAPI();" /><br>
        <input type="button" class="btn btn-primary" value="Call fetchStatus" onclick="javascript:fetchStatus();" /><br>

        <div id="mydiv" style="color:red"></div>
        <script type="text/javascript">
            var myIpAdresse;
            //Hent datamskinens IP adressen
            getIP()

            //************************************************************************************************************
            //************************************************************************************************************
            //**  variabler
            var url = 'https://frost.met.no/sources/v0.jsonld?types=SensorSystem&geometry=nearest(POINT(10.905166779011005%2060.060156717165675))';

            var x;
            var txt;

            var MyUserName = '0c23a1ce-1d3b-4481-9c0a-ecc505e39620';

            //var credentials= window.btoa(MyUserName + ':' + MySecret);
            var credentials= window.btoa(MyUserName + ':');
            var logData = makeBaseAuth(MyUserName, MySecret);
            const httpGetOptions ={withCredentials: true,};

            //alert("Authorization: Basic " + credentials);
            //alert(credentials);     //dXNlcm5hbWU6cGFzc3dvcmQ=

            //************************************************************************************************************
            //************************************************************************************************************
            //** Hent IP Adressen
            function getIP() {
                var MyIP = new XMLHttpRequest();

                MyIP.open('GET', 'https://api.ipify.org?format=json', true);

                MyIP.onload=function() {
                    // process response
                    if (MyIP.status == 200) {
                        // parse JSON data
                        var myObjIP = JSON.parse(MyIP.responseText);
                        myIpAdresse = myObjIP.ip;

                        document.getElementById('ip').innerHTML = myIpAdresse;

                    } else {
                        alert('Error!');
                    }
                };
                MyIP.send();
            }
            //************************************************************************************************************
            //************************************************************************************************************





            //************************************************************************************************************
            //************************************************************************************************************
            //** Hent data fra URL
            function CallWebAPI() {
                //var client = null;
                var url2 = new URL('/sources/v0.jsonld?types=SensorSystem', 'https://frost.met.no');
                url2=url2.toString();

                var url ='https://frost.met.no/sources/v0.jsonld?types=SensorSystem';


                var client = new XMLHttpRequest()
                client.open('GET', url, true)

                client.onload=function() {
                    // process response
                    if (client.status == 200) {
                        // parse JSON data
                        alert(JSON.parse(client.responseText));

                    } else {
                        alert('Error!');
                    }
                };

                client.setRequestHeader('Access-Control-request-Methods', 'GET');
                client.setRequestHeader('Content-Type', 'application/vnd.api+json');
                client.setRequestHeader('Authorization','Basic ' + credentials);
                client.send(null);

                client.onerror = function() {
                    alert('xmlHTTP Error', client.responseText)
                }
            }


            function fetchStatus() {
                var client = new XMLHttpRequest();

                client.onreadystatechange = function() {
                // in case of network errors this might not give reliable results
                if(this.readyState == 4)
                    returnStatus(this.status);
                }

                client.open('HEAD', url1);
                client.setRequestHeader('Accept','application/json');
                client.setRequestHeader('Authorization','Basic ' + credentials);
                client.send();
            }

            function makeBaseAuth(user,password) {
                var tok = user + ':' + password;
                var hash = btoa(tok);
                //alert(hash);
                return 'Basic ' + hash;
            }
        </script>

</body>
</html>

Upvotes: 1

Related Questions