Hello I have some probs to understand and to work with JSON.
Through an AJAX request I try to catch a large JSON string. The string can be found here:
the AJX looks as follows:
var $xhr;
function loadContent(href){
var href = href;
if($xhr !== undefined) { $xhr.abort(); }
$xhr = $.ajax({
url: href ,
success: function(data) {
$('#myJson').text($(data))
}
});
};
loadContent("https://services3.arcgis.com/rKOPqLnqVBkPP9th/arcgis/rest/services/Arbeitsmappe1/FeatureServer/0/query?where=Energietr_ger+%3D+%27Wind%27&outFields=*&f=pjson")
the console answers:
Error: Syntax error, unrecognized expression: { "objectIdFieldName" : "FID", "globalIdFieldName" : "",
"geometryType" : "esriGeometryPoint", "spatialReference" : { "wkid" : 102100, "latestWkid" : 3857 }, "fields" : [ { "name" : "Netzbetreiber", "type" : "esriFieldTypeString", "alias" : "Netzbetreiber", "sqlType" : "sqlTypeNVarchar", "length" : 256, "domain" : null, "defaultValue" : null }, { "name" : "Anlagenschl_ssel", "type" : "esriFieldTypeString", "alias" : "Anlagenschl�ssel", "sqlType" : "sqlTypeNVarchar", "length" : 256, "domain" : null, "defaultValue" : null }, { "name" : "Energietr_ger", "type" : "esriFieldTypeString", "alias" : "Energietr�ger", "sqlType" : "sqlTypeNVarchar", "length" : 256, "domain" : null, "defaultValue" : null }, { "name" : "Stadt", "t jquery.min.js:2
Thats the beginning of the jsonfile. And I dont know what to do because I need the json in this format to work with:
[{
"Netzbetreiber":"E.DIS AG",
"Anlagenschl�ssel":"E41860010000000000602072054200001",
"Energietr_ger":"Solar",
"Stadt":"Fredersdorf-Vogelsdorf",
"Postleitzahl":15370,
"Adresse":"Ringstr. 4",
"bla":"BB",
"Installierte Leistung":"2,4",
"EEG Strom":"2.235,00",
"Verg�tung":"1.044,86",
"vNNE":"34,42",
"Biomasseanlagen":"nein",
"Inbetriebnahmejahr":"24.01.08",
"Einspeisespannungsebene":"NS",
"FIELD15":"",
"bla2":"null",
"lat": "52",
"lng": "12.274"
},
{
"Netzbetreiber":"E.DIS AG",
"Anlagenschl�ssel":"E41860010000000000602072054200001",
"Energietr_ger":"Solar",
"Stadt":"Fredersdorf-Vogelsdorf",
"Postleitzahl":15370,
"Adresse":"Ringstr. 4",
"bla":"BB",
"Installierte Leistung":"2,4",
"EEG Strom":"2.235,00",
"Verg�tung":"1.044,86",
"vNNE":"54,42",
"Biomasseanlagen":"nein",
"Inbetriebnahmejahr":"24.01.08",
"Einspeisespannungsebene":"NS",
"FIELD15":"",
"bla2":"null",
"lat": "52",
"lng": "12.374"
}]
I tried parseJSON, stringify toString and so on. I tried to put the output data of the AJAX request into an div, span or pre tag but nothing helped.
Somebody here with an excellent idea?
Thanks, Falk
You are incorrectly creating a jQuery object from the JSON response and that's what causes the error. What exactly are you trying to achieve? If you just need to inspect the result, then you could log the data to the browser's console. Then open the console
tab in your browser's dev tools.
The following line of code should suffice.
console.log(JSON.parse(data));
To do the reverse (convert a JS object to JSON) you can use JSON.stringify()
.
var data = {};
data['Netzbetreiber'] = 'E.DIS AG';
console.log(JSON.stringify(data)); /* Outputs: {"Netzbetreiber":"E.DIS AG"} */
I have tried to fire the same query as you done and found that ,the JSON Response which comes back is around 50000 lines. As @aross mentioned JSON.parse(data) in the success works ,you can use this to create a javascript object and try use that object in the rest of application.
However my advice is to get a JSON response which is of less number of lines if possible. Putting this response(data) into a div is not working as javascript String implementation is slow and error -prone as mentioned here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays in the second paragraph.