FileMaker IF语句异常:遍历JSON时始终判定为True的问题
解决FileMaker中JSON遍历IF语句始终为True的问题
看起来你在遍历JSON数据映射到FileMaker字段时踩了几个语法和逻辑的坑,导致条件判断一直意外成立,所有数据都往同一个字段重复插入。我帮你拆解一下问题点,再给出修正后的代码:
核心问题分析
- JSON路径语法错误:你写的
"data[" & $i & ".field"和"data[" & $i & "]value"都是格式错误的——前者少了闭合的],后者把.写成了],导致JSONGetElement返回的不是预期值,FileMaker对错误值的比较逻辑会让条件意外成立。 - 循环变量未更新:你的循环里没有递增
$i,也没有退出循环的条件,会一直重复处理索引为0的JSON元素,同时因为条件判断失效,数据会重复插入。 - 冗余的条件嵌套:第二个
Else If里多了一层=1的嵌套,完全没必要,反而会打乱判断逻辑。 - 重复的条件判断:最后有一个和delegate重复的条件,会导致错误覆盖字段值。
修正后的代码
Set Variable [ $dataCount; Value:ValueCount ( JSONListKeys ( $json ; "data" ) ) ] Set Variable [ $i; Value:0 ] #needs if statement to revert changes if existing Go to Layout [ “grievances” (grievances) ] New Record/Request Set Field [ grievances::_IDgrievance; JSONGetElement ( $json ; "id" ) ] Set Field [ grievances::grievanceTimestampSubmitted; JSONGetElement ( $json ; "timestamp" ) ] Loop # 先递增循环变量,再处理(初始$i=0,第一次循环处理索引0) Set Variable [ $i; Value:$i + 1 ] Exit Loop If [ $i ≥ $dataCount ] Set Error Capture [ On ] # 修正JSON路径,统一用正确格式:data[索引].field/value If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = GetField ( jsonFieldIDs::institution ) ] Set Field [ grievances::institution; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = GetAsNumber (jsonFieldIDs::delegates) ] Set Field [ grievances::delegate; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::dateGrievanceOriginated ] Set Field [ grievances::dateGrievanceOriginated; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::grievanceStep ] Set Field [ grievances::grievanceStep; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::grievanceSubmissionID ] Set Field [ grievances::relatedGrievance; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::employeesName ] Set Field [ grievances::rawEmployeeName; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::employeePhone ] Set Field [ grievances::rawEmployeePhoneNumber; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::employeesAddress ] Set Field [ grievances::rawEmployeeAddress; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::email ] Set Field [ grievances::employeesEmail; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::employeesJobTitle ] Set Field [ grievances::employeesJobTitle; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::shift ] Set Field [ grievances::employeesShift; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::seniorityDate ] Set Field [ grievances::employeesSeniorityDate; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::department ] Set Field [ grievances::employeesDepartment; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::employeeSignature ] Set Field [ grievances::employeesSignature; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::natureOfGrievance ] Set Field [ grievances::grievanceNature; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::violationOfArticle ] Set Field [ grievances::grievanceArticle; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::section ] Set Field [ grievances::grievanceSection; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] Else If [ JSONGetElement ( $json ; "data[" & $i & "].field" ) = jsonFieldIDs::statementOfCase ] Set Field [ grievances::grievanceCaseStatement; JSONGetElement ( $json ; "data[" & $i & "].value" ) ] End If End Loop
额外调试建议
如果还是有问题,可以在循环开头加一个Show Custom Dialog [ "当前字段标识"; JSONGetElement($json; "data[" & $i & "].field") ],看看每次获取的field值和你预期的jsonFieldIDs里的内容是否匹配,这样能快速定位条件不生效的原因。
内容的提问来源于stack exchange,提问作者Dskip1199
相关产品推荐
相关产品推荐

