var queryA = new esri.tasks.Query(); queryA.where = myGeogString; //myGeogString declared earlier and has a multiple query string ("State_name"='xyz' OR "State_name"='abc' .....) queryA.returnGeometry = false; queryA.outFields = ["*"]; var results; queryTask.execute(queryA, function(featureSet) { var attr = [], items = []; for ( i = 0, il = featureSet.features.length; i < il; i++) { var features = featureSet.features; var fattributes = features.attributes; results = []; for (att in fattributes) { for ( k = valueB.length; k > -1; k--) { //length of array of indicators for each feature in myGeogString if (att == valOutB) { var myInd = valueB ; var myAttr = fattributes[att]; results.push(myInd + ":" + myAttr); } } } items.push("location:"+featureSet.features.attributes.State_name + "," + results); } var itemsJson = JSON.stringify({locations:items}); alert(itemsJson); });
Solved! Go to Solution.
What is it you're trying to match? Do you want a JSON representation of the result featureSet? If so, you can do this:
var featureJSON = dojo.toJson(featureSet.toJson()); alert(featureJSON);
In your code, the items array is going to contain a list of plain string values...when this gets converted to a string, it will not look much like the original featureSet object data.
var queryA = new esri.tasks.Query(); queryA.where = myGeogString; //myGeogString declared earlier and has a multiple query string ("State_name"='xyz' OR "State_name"='abc' .....) queryA.returnGeometry = false; queryA.outFields = ["*"]; var results; queryTask.execute(queryA, function(featureSet) { var attr = [], items = []; for ( i = 0, il = featureSet.features.length; i < il; i++) { var features = featureSet.features; var fattributes = features.attributes; results = []; for (att in fattributes) { for ( k = valueB.length; k > -1; k--) { //length of array of indicators for each feature in myGeogString if (att == valOutB) { var myInd = dojo.toJson(valueB ); var myAttr = dojo.toJson(fattributes[att]); results.push(myInd + ":" + myAttr); } } } var loc = dojo.toJson(featureSet.features.attributes.State_name); items.push('{"location":'+loc + "," + results+"}"); } var locStr = dojo.toJson("locations"); var itemsJson = "{"+locStr+":["+items+"]}"; alert(itemsJson); });
Hi,
I am using ArcGIS Server 10.1 with JavaScript 3.2.
I am running a query on a feature layer and the output is a JavaScript array. I would like the output to be in JSON. I have tried using JSON.stringify(), but the output does not match with the output I get using REST endpoint. Here is my code:var queryA = new esri.tasks.Query(); queryA.where = myGeogString; //myGeogString declared earlier and has a multiple query string ("State_name"='xyz' OR "State_name"='abc' .....) queryA.returnGeometry = false; queryA.outFields = ["*"]; var results; queryTask.execute(queryA, function(featureSet) { var attr = [], items = []; for ( i = 0, il = featureSet.features.length; i < il; i++) { var features = featureSet.features; var fattributes = features.attributes; results = []; for (att in fattributes) { for ( k = valueB.length; k > -1; k--) { //length of array of indicators for each feature in myGeogString if (att == valOutB) { var myInd = valueB ; var myAttr = fattributes[att]; results.push(myInd + ":" + myAttr); } } } items.push("location:"+featureSet.features.attributes.State_name + "," + results); } var itemsJson = JSON.stringify({locations:items}); alert(itemsJson); });
Is it possible to convert this output to JSON so it matches with REST output?
Thanks
Samir
var featureJSON = dojo.toJson(featureSet.toJson()); alert(featureJSON);
What is it you're trying to match? Do you want a JSON representation of the result featureSet? If so, you can do this:
var featureJSON = dojo.toJson(featureSet.toJson()); alert(featureJSON);
In your code, the items array is going to contain a list of plain string values...when this gets converted to a string, it will not look much like the original featureSet object data.
var queryA = new esri.tasks.Query(); queryA.where = myGeogString; //myGeogString declared earlier and has a multiple query string ("State_name"='xyz' OR "State_name"='abc' .....) queryA.returnGeometry = false; queryA.outFields = ["*"]; var results; queryTask.execute(queryA, function(featureSet) { var attr = [], items = []; for ( i = 0, il = featureSet.features.length; i < il; i++) { var features = featureSet.features; var fattributes = features.attributes; results = []; for (att in fattributes) { for ( k = valueB.length; k > -1; k--) { //length of array of indicators for each feature in myGeogString if (att == valOutB) { var myInd = dojo.toJson(valueB ); var myAttr = dojo.toJson(fattributes[att]); results.push(myInd + ":" + myAttr); } } } var loc = dojo.toJson(featureSet.features.attributes.State_name); items.push('{"location":'+loc + "," + results+"}"); } var locStr = dojo.toJson("locations"); var itemsJson = "{"+locStr+":["+items+"]}"; alert(itemsJson); });