ich folgende JSON Struktur haben:Sorting JSON-Daten in Ruby
{
"name": "MessageEnvelope",
"type": "record",
"fields": [
{
"name": "message",
"type":
{
"name": "Message",
"type": "record",
"fields": [
....
]
}
},
{
"name": "pipeline_system",
"type": {
"type": "enum",
"name": "PipelineSystem",
"symbols": [ "enterprise", "backscoring", "compliance" ]
}
},
{
"name": "batch_id",
"type": [ "null", "string" ]
}
]
}
Ich suche die oben JSON-Datei, so gut wie es sein könnte zu sortieren. Zum Beispiel:
fields : [
{
"name": "batch_id",
"type": [ "null", "string" ]
},
...
...
{
"name": "pipeline_system",
"type": {
"type": "enum",
"name": "PipelineSystem",
"symbols": [ "backscoring", "compliance", "enterprise" ]
}
}
Wie es sortiert die internen Arrays sowie Hashes. Ich versuche folgendes zu schreiben:
def sort(collection)
if collection.is_a?(Hash)
puts "Hash Object...."
if(collection["type"]=="record")
puts "record found... Type = #{collection["fields"].class}";
if collection["fields"].is_a?(Array)
puts "fields type is array...." #we can sort fields arrays on the basis of name
collection["fields"].sort_by{|arrayCollectionElement| arrayCollectionElement["name"] }
arrayCollection = collection["fields"] #this is array of hash...we can sort them on the basis of name..done above in sort by...
puts "class = #{arrayCollection.class}"
puts "sorted fields: #{arrayCollection}"
end
end #else it is again a hash
end
collection
end
aber es ist nicht sortiert die Felder Array auf der Grundlage von Namen.
Schätzen Sie jede mögliche Hilfe!
Nicht nur Namen, sondern auch Arrays innerhalb des Hash sollten sortiert werden. Wie "Symbole": ["Backscoring", "Compliance", "Unternehmen"] ' – scorix
@scorix hat es, aktualisiert. – mudasobwa
@scorix ich Array von Hashes bekommen konnten auch mögen: ' "type": [ "null", {SOME_HASH}, { SOME_HASH} ] ' derzeit wenn Array dann v.sort! wird nur das Array von Strings sortieren. –