冷熔解液
我在代码中为您添加了注释。
<cfscript>
docFromDB = '{
"product123_types": {
"type_1": {
"settings": {
"name": "something",
"success": "600,400,500,800"
}
}
},
"product345_types": {
"type_1": {
"settings": {
"name": "something",
"success": "500,400"
}
},
"type_2": {
"settings": {
"name": "another one",
"success": "500,800,700"
}
}
}
}'; // assuming you are fetching the JSON as string from the database
// deserialize JSON string from database
docFromDB = deserializeJSON(docFromDB);
// iterate over each entry in the JSON
for (key in docFromDB) {
// skip all keys that do not match our pattern "productN_types"
if (!reFind("^product[0-9]+_types$", key)) {
continue;
}
// alias to reference the "productN_types" node
productNode = docFromDB[key];
// iterate over each entry in the "productN_types" node
for (key in productNode) {
// skip all keys that do not match our pattern "type_N"
if (!reFind("^type_[0-9]+$", key)) {
break;
}
// alias to reference the "type_N" node in the "productN_types" node
typeNode = productNode[key];
// skip node if there is no "settings" key in it
if (!structKeyExists(typeNode, "settings")) {
break;
}
// alias to reference the "settings" node in the "type_N" node of the "productN_types" node
settingsNode = typeNode["settings"];
// skip node if there is no "success" key in it
if (!structKeyExists(settingsNode, "success")) {
break;
}
// success values as "list" (comma separated)
successValueList = settingsNode["success"];
// success values as "array"
successValueArray = listToArray(successValueList); // exactly what String.split(',') in JS does
// iterate over each success value (we are using "i" to reference the entry later)
i = 1;
for (successValue in successValueArray) {
// do your actual lookup, I'm just mocking something here
if (successValue == "400") {
// we are using the current index "i" to overwrite the array value,
// because "successValue" is just a flat copy here
successValueArray[i] = "fourhundred";
}
i++;
}
// write back your modified success values,
// "settingsNode" is a reference, so we can simply overwrite it
settingsNode["success"] = arrayToList(successValueArray); // exactly what String.join(',') in JS does
}
}
// your result
writeDump(docFromDB);
</cfscript>