如何在ColdFusion

时间:2016-11-09 13:52:13

标签: coldfusion

我有一个名为jsonJobsOpen的对象,当我转储它时,输出显示此Object的值,如下所示。

[{
    "APPLICATIONCOUNT":0,
    "JOBNAME":"Candidate Migration Job ",
    "JOBID":"15433AAE-8631-FA8B-AEA7-49E116EF60E1 ",
    "JOBCLOSEDATE":" ",
    "JOBSTATUSLABELVALUE":"Incomplete",
    "ENCRYPTEDURLAPPCOUNT":"event=cms.listApplicantsPersistVars&JobID=15433AAE-8631-FA8B-AEA7-49E116EF60E1&JobAppStatusID=DCA85CD5-A134-7A4B-30E8-49E116F0D702",
    "JOBOPENDATE":" ",
    "APPLIEDAPPCOUNT":0,
    "JOBSTATUSLABELCOLOUR":"D8605F",
    "REFERENCE":"Candidate Migration Job ",
    "ENCRYPTEDURL":"event=jm.gotoJCW&JobID=15433AAE-8631-FA8B-AEA7-49E116EF60E1",
    "ENCRYPTEDURLCANDIDATEASSESSMENT":"event=jm.gotoJCW&JobID=15433AAE-8631-FA8B-AEA7-49E116EF60E1",
    "ENCRYPTEDURLAPPLICANTLIST":"event=cms.listApplicantsPersistVars&JobID=15433AAE-8631-FA8B-AEA7-49E116EF60E1"
}]

如果没有与Object关联的值,则转储如下所示:

[{
    "JOBID":"",
    "JOBOPENDATE":"",
    "JOBCLOSEDATE":"",
    "JOBNAME":"",
    "REFERENCE":"",
    "ENCRYPTEDURL":"",
    "APPLICATIONCOUNT":"",
    "ENCRYPTEDURLAPPLICANTLIST":"",
    "ENCRYPTEDURLCANDIDATEASSESSMENT":"",
    "JOBSTATUSLABELVALUE":"",
    "JOBSTATUSLABELCOLOUR":"",
    "APPLIEDAPPCOUNT":"",
    "ENCRYPTEDURLAPPCOUNT":""
}]

我的问题是我可以使用什么条件来检查ColdFusion中此对象的值是否存在?

1 个答案:

答案 0 :(得分:2)

From Leigh's answer here

  

在JSON中,[]表示数组,{}表示结构(或对象)。所以你的输入实际上是一个结构数组。你需要使用数组循环......

<cfset arrayOfStructs = deserializeJson(jsonJobsOpen)>
<cfloop array="#arrayOfStructs#" index="thisValue">
    <!--- Now you can use this condition to check for empty values --->
    <cfif StructKeyExists(thisValue, "JOBID") and Len(thisValue.JOBID) GT 0>
    ... 
</cfloop>

我在那里做了几件事。首先将JSON字符串反序列化为ColdFusion可以处理的事物(结构数组)。然后检查JOBID字段是否存在。如果它确实存在,那么还要检查它的长度是否大于0(非空)。

您应该可以为每个元素执行类似的操作(假设包含此JSON的变量的名称为jsonJobsOpen

以下是一个快速ColdFusion Gist,其中显示了使用您的数据的示例。