我正在处理此代码,需要按一个值(在本例中称为cars)对JSON输出进行排序。如何在此代码中完成?
<ItemGroup>
<Compile Include="C:\Users\kirst\AppData\Local\JetBrains\Shared\vAny\DecompilerCache\decompiler\long number\bd\long number\ProductionWorkflowStatus.cs" Link="ProductionWorkflowStatus.cs" />
</ItemGroup>
答案 0 :(得分:0)
我认为这段代码将为您提供所需的结果:
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "test.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
var array = [];
for (var i = 0; i <= lines.length; i++) {
array.push(JSON.parse(lines[i]))
}
array.sort(function(a, b){
if (a._serverMessages && a._serverMessages.length && a._serverMessages[0] && a._serverMessages[0].car) {
if (b._serverMessages && b._serverMessages.length && b._serverMessages[0] && b._serverMessages[0].car) {
return a._serverMessages[0].car > b._serverMessages[0].car? 1 : -1;
} else return -1;
} else return 1;
});
for (var i = 0; i <= array.length; i++) {
document.getElementById("demo").innerHTML += array[i]._serverMessages[0].car + " " + array[i].creator.substr(2, array[i].creator.length) + " / " + array[i]._serverMessages[0].content + "<br>";
}
}
}
}
txtFile.send(null);