2016-07-13 5 views
1

Ich habe eine Liste wie [gender, axonVoucherCode] Auch ich habe einen JSON-Array [[gender:Gender?], [concussionHistory:History of previous concussion?], [previousConcussion:Number of previous concussions?], [historyMigraineChronic:History of migraine or chronic headaches?], [edTreatment:ED treatment ?], [axonVoucherCode:Axon Voucher Code ?]]Such Daten in JSON-Array und setzen Sie in in Array-Liste in Grails

Ich mag wie [Gender?,Axon Voucher Code ?] eine Liste mit entsprechendem Wert der ersten Liste machen .Ich Verwenden Sie JsonSlurper zum Analysieren von Json.

def fetchQuestion(def list){ 
     def webRootDir = SCH.servletContext.getRealPath("/") 
     def f = new File(webRootDir + "/jsons/" + "QuestionPart1") 
     def questionList = new JsonSlurper().parseText(f.text) 
     def newlists=[] 
     println questionList.QuestionPart1 
     questionList.QuestionPart1.each{ 
      println(it) 
     } 

     println(newlists); 
     return newlists 
    } 
    //I want to put matching value to newlists 

Hier ist mein JSON-Dateiformat.

{"QuestionPart1":[ 
    {"gender":"Gender?"}, 
    {"concussionHistory":"History of previous concussion?"}, 
    {"previousConcussion":"Number of previous concussions?"}, 
    {"historyMigraineChronic":"History of migraine or chronic headaches?"}, 
    {"edvisitTime":"Date and time of ED visit?"}, 
    {"injuryTime":"Date and time of Injury?"}, 
    {"mechanismInjury":"Mechanism of Injury?"}, 
    {"sportsType":"Choose Sport?"}, 
    {"signAndSymptom":"Select the signs and symptoms the subject experienced following injury?"}, 
    {"durationLossConsciousness":"Duration of loss of Conciousness ? "}, 
    {"durationBeforeAmnesia":"Duration of Amnesia for events BEFORE injury ?"}, 
    {"durationAfterAmnesia":"Duration of Amnesia for events AFTER injury ?"}, 
    {"ctObtainedED":"Head CT obtained in ED ?"}, 
    {"edTreatment":"ED treatment ?"}, 
    {"axonVoucherCode":"Axon Voucher Code ?"} 
]} 

Bitte helfen Sie mir dieses Problem

Ich verstehe Ihre Frage .Dank zu lösen
+0

gleich wollen Sie die Werte von allen Einträgen in der Liste? Einfach die Frage klären –

+0

Ich brauche nur übereinstimmende Werte, um neue Liste zu machen. – Sayem

Antwort

1

Unter der Annahme, sollte diese Arbeit:

def fetchQuestion(String ...keys){ 
    def webRootDir = SCH.servletContext.getRealPath("/") 
    def f = new File(webRootDir + "/jsons/" + "QuestionPart1") 
    def questionList = new JsonSlurper().parseText(f.text) 
    def newlists=[] 
    println questionList.QuestionPart1 
    questionList.QuestionPart1.findResults { it.find { k, v -> k in keys }?.value } 
} 

def listOfValues = fetchQuestion('edvisitTime', 'axonVoucherCode') 

Hier listOfValues['Date and time of ED visit?', 'Axon Voucher Code ?']