ColdFusion - 如何找到始终在某些字符前后的字符串

时间:2016-02-04 23:44:14

标签: coldfusion

假设我有一个字符串如下:

  

position“:1,”type“:”row“,”answer_id“:”9541943203“},{”text“:”创建报告或视图“,”可见“:true,”position“:2, “type”:“row”,“answer_id”:“9541943204”},{“text”:“编辑报告或视图”,“可见”:true,“position”:3,“type”:“row”, “answer_id”:“9541943205”},{“text”:“保存报告或视图”,“可见”:true,“position”:4,“type”:“row”,“answer_id”:“9541943206”} ,

如何获取每个answer_id的值?

我知道我想要的价值总是先于"answer_id":",而且始终跟着"},

如何编制这些值的列表?

e.g。 9541943203,9541943204,9541943205,9541943206

反序列化JSON的转储: CFDump - Deserialized JSON

1 个答案:

答案 0 :(得分:3)

您可以像在Javascript中一样使用ColdFusion中的JSON(几乎)。

<!--- your JSON string here --->
<cfset sourceString = '{ "data": { ... } }'>

<!--- we will add all answer_id to this array --->
<cfset arrayOfAnswerIDs = []>

<!--- in case there went something wrong, we will store the reason in this variable --->
<cfset errorMessage = "">

<cftry>

    <!--- deserialize the JSON to work with for ColdFusion --->
    <cfset sourceJSON = deserializeJSON(sourceString)>

    <!--- validate the structure of the JSON (expected keys, expected types) --->
    <cfif (
        structKeyExists(sourceJSON, "data") and
        isStruct(sourceJSON["data"]) and
        structKeyExists(sourceJSON["data"], "pages") and
        isArray(sourceJSON["data"]["pages"])
    )>

        <!--- iterate pages --->
        <cfloop array="#sourceJSON["data"]["pages"]#" index="page">

            <!--- skip pages that do not contain questions --->
            <cfif (
                (not isStruct(page)) or
                (not structKeyExists(page, "questions")) or
                (not isArray(page["questions"]))
            )>
                <cfcontinue>
            </cfif>

            <!--- iterate questions --->
            <cfloop array="#page["questions"]#" index="question">

                <!--- skip questions that do not have answers --->
                <cfif (
                    (not isStruct(question)) or
                    (not structKeyExists(question, "answers")) or
                    (not isArray(question["answers"]))
                )>
                    <cfcontinue>
                </cfif>

                <!--- iterate answers --->
                <cfloop array="#question["answers"]#" index="answer">

                    <!--- skip invalid answer objects --->
                    <cfif not isStruct(answer)>
                        <cfcontinue>
                    </cfif>

                    <!--- fetch the answer_id --->
                    <cfif (
                        structKeyExists(answer, "answer_id") and
                        isSimpleValue(answer["answer_id"])
                    )>

                        <!--- add the answer_id to the array --->
                        <cfset arrayOfAnswerIDs.add(
                            answer["answer_id"]
                        )>

                    </cfif>

                </cfloop>

            </cfloop>

        </cfloop>

    <cfelse>
        <cfset errorMessage = "Pages missing or invalid in JSON.">
    </cfif>

    <cfcatch type="Application">
        <cfset errorMessage = "Failed to deserialize JSON.">
    </cfcatch>
</cftry>

<!--- show result in HTML --->
<cfoutput>

    <!--- display error if any occured --->
    <cfif len(errorMessage)>
        <p>
            #errorMessage#
        </p>
    </cfif>

    <p>
        Found #arrayLen(arrayOfAnswerIDs)# answers with an ID.
    </p>

    <ol>
        <cfloop array="#arrayOfAnswerIDs#" index="answer_id">
            <li>
                #encodeForHtml(answer_id)#
            </li>
        </cfloop>
    </ol>

</cfoutput>

您可能希望在处理过程中跟踪所有意外跳过,因此请考虑使用错误数组而不是单个字符串。

如果您想要列表中的answer_id,请使用
arrayToList(arrayOfAnswerIDs, ",")