Javascript array to JSON

1399
3
Jump to solution
02-22-2013 04:51 AM
SamirGambhir
Occasional Contributor III
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
0 Kudos
1 Solution

Accepted Solutions
SamirGambhir
Occasional Contributor III
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.


Thanks Jeff and Mike,
Since I needed the output string to be formatted in JSON, Graphic.toJson() or featureSet.toJson() did not apply to my situation for now, but it be of help later. So thanks for your inputs. What worked for me was to convert each item to Json before I combined all those together: Here is the revised 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 = 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);     });

View solution in original post

0 Kudos
3 Replies
JeffJacobson
Occasional Contributor III
See the Graphic.toJson function.

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
0 Kudos
by Anonymous User
Not applicable
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.
0 Kudos
SamirGambhir
Occasional Contributor III
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.


Thanks Jeff and Mike,
Since I needed the output string to be formatted in JSON, Graphic.toJson() or featureSet.toJson() did not apply to my situation for now, but it be of help later. So thanks for your inputs. What worked for me was to convert each item to Json before I combined all those together: Here is the revised 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 = 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);     });
0 Kudos