英文:
formatting json in Coldfusion
问题
SerializeJSON的输出看起来是这样的:
{"COLUMNS":["ZIPCODE","COLOR"],"DATA":{"54814":"#3269B7","60050":"#DC3E08"}}
这个格式将邮政编码作为键,颜色代码作为数据值进行了格式化。
英文:
I am at a loss on the SerializeJSON
output of a CFQUERY. The below code:
<cfsetting showdebugoutput="yes">
<cfheader name="Content-Type" value="application/json">
<cftry>
<cfquery name="GetData" datasource="dsn">
with cte as (
select distinct ZipCode from db.dbo.table1 where ZipCode in (#URL.Zip#)
)
SELECT
cte.ZipCode
, '##' + CONVERT(VARCHAR(max), CRYPT_GEN_RANDOM(3), 2) as Color
from cte
</cfquery>
<cfoutput>
#SerializeJSON(GetData)#
</cfoutput>
<cfcatch type="any">
Error: <cfoutput>#cfcatch.message#</cfoutput>
</cfcatch>
</cftry>
creates this output:
{"COLUMNS":["ZIPCODE","COLOR"],"DATA":[["54814","#3269B7"],["60050","#DC3E08"]]}
when the #URL.ZIP#
variable = 60050,54814
I need the DATA
component to have the JSON formatted with the zip code as a key and the color code as a data value.
How do I tell SerializeJSON
I need it that way?
Thanks
答案1
得分: 5
你可以使用SerializeJSON(data[, queryFormat[, useSecureJSONPrefix[, useCustomSerializer]]])
中的第二个参数(queryFormat
)。
serializeJSON(GetData, 'STRUCT')
结果会类似于这样。
[{"ZIPCODE": "54814","COLOR": "#3269B7"}, {"ZIPCODE": "60050","COLOR": "#DC3E08"}]
英文:
You can you the 2nd argument(queryFormat
) in SerializeJSON(data[, queryFormat[, useSecureJSONPrefix[, useCustomSerializer]]])
serializeJSON(GetData, 'STRUCT')
The result will look something like this.
>[{"ZIPCODE": "54814","COLOR": "#3269B7"}, {"ZIPCODE": "60050","COLOR": "#DC3E08"}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论