1

Hello guys I am having some trouble trying to parse out the returned JSON data from a dijit search. I have the following code where s.set("value","48035") will be populated from another page so for the time being i manually set it to 48035.

 var s = new Search({
 enableButtonMode: true, //this enables the search widget to display as a single button
 enableLabel: false,
 enableInfoWindow: true,
 showInfoWindowOnSelect: false,
 map: map
 }, "search");
 s.set("value", "48035");
//have tried the following
 s.search().then(function (response) {
 var extra = response[0][0].x
 alert(extra)
 });
//and the following
s.search().then(function (response) {
 var extra = response.candidates[0].location
 alert(extra.x)
 });
 s.startup();

It seems like no matter what i try or do i can not figure out how to get the returned value of x. Here is what the returned JSON data looks like.

{"spatialReference":{"wkid":4326,"latestWkid":4326},"candidates":[{"address":"48035, Clinton Township
, Michigan","location":{"x":-82.9172080701955,"y":42.554260928999781},"score":100,"attributes":{"Addr_type"
:"Postal","Match_addr":"48035, Clinton Township, Michigan","StAddr":"","City":"Clinton Township"},"extent"
:{"xmin":-82.922208999999995,"ymin":42.549261000000001,"xmax":-82.912209000000004,"ymax":42.559260999999999
}}]}
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 11, 2015 at 14:04
0

1 Answer 1

1

You should import the json module from Dojo. It will allow you to create a JavaScript object from your return JSON and do what you want with it.

So, if you want to create a Geometry object, you can parse the returned JSON with the json module, get your JavaScript object, and create it from the properties of the derived JavaScript object.

Say your object is named returnJSON and you want to create a Point from it.

var returnJSON = JSON.parse(response.candidates[0]);
var x = returnJSON.location.x;
var y = returnJSON.location.y;
//then create your point or whatever you want to do
answered Aug 11, 2015 at 14:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.