简体   繁体   中英

Get and parse a JSON-String from URL

How can i get this json-data-string as an variable and parse it into some usefull fromat?

The url for the string: http://intranet.ooelfv.at/webext2/getjson.php?scope=laufend&callback=?

Iam pretty new to js and json so some advice on how to get into this topic would be great.

Thank you, Stoani

Here's a simple runable example that just logs the content of the data to your console

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function getData(jsonp){
        console.log(jsonp);
    }

    var scripts = document.createElement('script');
    scripts.src = 'http://intranet.ooelfv.at/webext2/getjson.php?scope=laufend&callback=getData';
    document.body.appendChild(scripts);
</script>
</body>
</html>

You can access the data by modifying the getData function. For example, to get the title, you can use

function getData(jsonp){
    console.log(jsonp.title);
}

There are some errors in the formation of their return string.
I made some adjustments in your return string copying and pasting it on this site .
I think you need something to get the keys and their respective values:

    var data = '{"webext2":true,"version":"1.2","title":"laufende Eins\u00e4tze","pubDate":"Mon, 03 Oct 2016 19:01:02 +0200","cnt_feuerwehren":3,"cnt_einsaetze":2,"einsaetze":{"0":{"einsatz":{"num1":"E161000221","einsatzort":"PE - PABNEUKIRCHEN","startzeit":"Mon, 03 Oct 2016 18:24:17 +0200","inzeit":"","status":"offen","alarmstufe":1,"einsatzart":"BRAND","einsatztyp":{"id":"BK","text":"BRAND KLEIN FEUERWEHREINSATZ"},"einsatzsubtyp":{"id":"KAMIN-BK","text":"BRAND KAMIN"},"adresse":{"default":"NEUDORF 21","earea":"NEUDORF","emun":"PABNEUKIRCHEN","efeanme":"NEUDORF","estnum":"21","ecompl":""},"wgs84":{"lng":14.829464095886,"lat":48.349240803097},"bezirk":{"id":7,"text":"Perg"},"cntfeuerwehren":2,"feuerwehren":{"407107":{"feuerwehr":"FF Pabneukirchen"},"407109":{"feuerwehr":"FF Riedersdorf"}}}},"1":{"einsatz":{"num1":"E161000213","einsatzort":"RI - RIED IM INNKREIS","startzeit":"Mon, 03 Oct 2016 17:57:39 +0200","inzeit":"","status":"offen","alarmstufe":1,"einsatzart":"TEE","einsatztyp":{"id":"TK","text":"TECHNISCH KLEIN FEUERWEHREINSATZ"},"einsatzsubtyp":{"id":"OELSPUR-TK","text":"\u00d6LSPUR, \u00d6LAUSTRITT"},"adresse":{"default":"RIED IM INNKREIS","earea":"RIED IM INNKREIS","emun":"RIED IM INNKREIS","efeanme":"","estnum":"","ecompl":"SCH\u00c4RDINGER TOR"},"wgs84":{"lng":13.488453771887,"lat":48.212407174905},"bezirk":{"id":8,"text":"Ried"},"cntfeuerwehren":1,"feuerwehren":{"408223":{"feuerwehr":"FF Ried im Innkreis"}}}}}}';
    var jsondata = JSON.parse(data);
    check_json_data(jsondata);

    function check_json_data(jsondata) {
        for (var propertyName in jsondata) {
            if (propertyName) {
                if (typeof jsondata[propertyName] == 'object') {
                    check_json_data(jsondata[propertyName]);
                }
                else {
                    console.log(propertyName + ": " + jsondata[propertyName])
                }
            }
        }
    }

Output:
webext2: true
version: 1.2
title: laufende Einsätze
pubDate: Mon, 03 Oct 2016 19:01:02 +0200
cnt_feuerwehren: 3
cnt_einsaetze: 2
num1: E161000221
einsatzort: PE - PABNEUKIRCHEN
startzeit: Mon, 03 Oct 2016 18:24:17 +0200
inzeit:
status: offen
alarmstufe: 1
einsatzart: brAND
id: BK
text: brAND KLEIN FEUERWEHREINSATZ
id: KAMIN-BK
text: brAND KAMIN
default: NEUDORF 21
earea: NEUDORF
emun: PABNEUKIRCHEN
efeanme: NEUDORF
estnum: 21
ecompl:
lng: 14.829464095886
lat: 48.349240803097
id: 7
text: Perg
cntfeuerwehren: 2
feuerwehr: FF Pabneukirchen
feuerwehr: FF Riedersdorf
num1: E161000213
einsatzort: RI - RIED IM INNKREIS
startzeit: Mon, 03 Oct 2016 17:57:39 +0200
inzeit:
status: offen
alarmstufe: 1
einsatzart: TEE
id: TK
text: TECHNISCH KLEIN FEUERWEHREINSATZ
id: OELSPUR-TK
text: ÖLSPUR, ÖLAUSTRITT
default: RIED IM INNKREIS
earea: RIED IM INNKREIS
emun: RIED IM INNKREIS
efeanme:
estnum:
ecompl: SCHÄRDINGER TOR
lng: 13.488453771887
lat: 48.212407174905
id: 8
text: Ried
cntfeuerwehren: 1
feuerwehr: FF Ried im Innkreis

i think you can use ajax.

    $.ajax({
            url: "ajaxurl.php or whatever",
            type: "POST",
            data: {
              data:datayouwattosend,    
            },
            async: false,
            statusCode: {
                404: function () {
                    alert("not found");
                }
            },
            success: function (data) {
                 console.log(data);
            }
     })

here is a complete example where you can find information about this.

http://api.jquery.com/jquery.getjson/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM