我正在执行ajax调用并返回json格式的数据。
var ColorName = json.features[r].attributes.ColorName;
Chrome将值显示为“0,255,0,204”,对应于R,G,B,A(A为透明度) http://cl.ly/image/362k1K083o3c/ColorName%20value.jpg
我想做的是将ColorName
传递给下面的对象:
var symbol = new SimpleMarkerSymbol({
"color": [ColorName],
"size": 12,
"angle": -30,
"xoffset": 0,
"yoffset": 0,
"type": "esriSMS",
"style": "esriSMSCircle",
"outline": {
"color": [0,0,0,255],
"width": 1,
"type": "esriSLS",
"style": "esriSLSSolid"
}
});
如果我将值硬编码到对象中,它可以正常工作,但是当我将其传入时,如上所示。我假设这是一个语法问题,因为其他一切正常,我没有收到任何错误。
============更新=================
以下是FireFox中显示的json格式数据片段
{
"recordsReturned":"213",
"status":"pass",
"msg":"213 record(s) found",
"geometryType" : "esriGeometryPoint",
"spatialReference" : {
"wkid" : 4326
},
"features" : [
{
"attributes" : {
"name" : "XYZ Medical Center",
"statusID" : "1",
"NamedColor":"0,255,0,204"
},
"geometry" : {
"x" : -77.4954450,
"y" : 38.2170020
}
},
{
"attributes" : {
"name" : "ABC Medical Center",
"statusID" : "1",
"NamedColor":"0,255,0,204"
},
"geometry" : {
"x" : -76.3069444444,
"y" : 36.8447222222
}
},
这是循环返回的json对象的片段:
if (json.status == 'pass'){
for ( var r = 0; r < json.recordsReturned;r=r+1){
var ColorName = json.features[r].attributes.NamedColor;
...
我希望这会有所帮助......
答案 0 :(得分:0)
非常微妙的区别,但当我改变它时:
var symbol = new SimpleMarkerSymbol({
**"color": ColorName,**
"size": 12,
"angle": -30,
"xoffset": 0,
"yoffset": 0,
"type": "esriSMS",
"style": "esriSMSCircle",
"outline": {
"color": [0,0,0,255],
"width": 1,
"type": "esriSLS",
"style": "esriSLSSolid"
}
});
然后更改了以下内容
"attributes" : {
"name" : "XYZ Medical Center",
"statusID" : "1",
**"NamedColor":"[0,255,0,204]"**
},
这一切都奏效了。回想一下,我猜JavaScript / jQuery将[]视为一个有意义的对象。