在Coldfusion中格式化JSON

huangapple go评论45阅读模式
英文:

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"}]

huangapple
  • 本文由 发表于 2023年5月18日 09:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定