-2

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

  • This looks like a JSONP transaction. Check out these SO questions: http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about/6879319#6879319 http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 – LinuxDisciple Oct 03 '16 at 17:05
  • Thank you, this seems kinda usefull. – Fabian Steinkogler Oct 03 '16 at 17:11

3 Answers3

0

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);
}
baao
  • 71,625
  • 17
  • 143
  • 203
  • This is the pure JavaScript implementation. None of the steps are hidden or abstracted away as they are in JQuery, so it's a good way to understand the whole problem. If you're not interested in understanding the problem and you just want to get the data into a variable, the JQuery approach is exactly what you want. – LinuxDisciple Oct 03 '16 at 17:22
0

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

Sr Julien
  • 494
  • 1
  • 8
  • 27
-2

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/

bjesua
  • 319
  • 1
  • 4
  • 14
  • You'll need to load the JQuery library to make this work. Using JQuery is far more user friendly than the pure JavaScript approach. – LinuxDisciple Oct 03 '16 at 17:20