Ich habe dieses JSONString in For-Schleife
{
"elements": [{
"company": "Microsoft",
"isValid": "yes"
}, {
"company": "Google",
"isValid": "no"
}, {
"company": "Apple",
"isValid": "yes"
}
]
}
Und ich möchte einen String Ausgabe erstellen, die wie dieser ist
Microsoft is OK
Google is NOT OK
Apple is OK
Wenn isValid = ja, dann ist es nicht in Ordnung ". Else ist es 'OK'
Hier mein Code
for (i in result) {
var output = new StringBuilder();
if (result[i].isValid == "yes") {
output.append(result[i].company);
output.append(" is OK\n");
} else {
output.append(result[i].company);
output.append(" is NOT OK\n");
}
}
console.log(output.toString());
aber Ausgang ist nur die letzte (Apple-OK ist);
Bitte helfen
Nicht sehr effizient, da Sie jedes Mal, wenn Sie die Schleife eingeben, einen neuen StringBuilder erstellen müssen. – Lapacho
Ja! Deine Antwort ist optimaler. –