2016-12-12 22 views
1

Ich habe ein JSON-Objekt in der Form:Extract Felder aus JSON

{"apps":{"app":[{"id":"application_1481567788061_0002","user":"root","name":"wordcount.py","queue":"default","state":"FAILED","finalStatus":"FAILED","progress":0.0,"trackingUI":"History", "diagnostics":"Application application_1481567788061_0002 failed 2 times due to AM Container for appattempt_1481567788061_0002_000002 exited with exitCode: 255\nFor more detailed output, check application tracking page:http://sandbox:8088/proxy/application_1481567788061_0002/Then, click on links to logs of each attempt.\nDiagnostics: Exception from container-launch.\nContainer id: container_1481567788061_0002_02_000001\nExit code: 255\nStack trace: ExitCodeException exitCode=255: \n\tat org.apache.hadoop.util.Shell.runCommand(Shell.java:538)\n\tat org.apache.hadoop.util.Shell.run(Shell.java:455)\n\tat org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:715)\n\tat org.apache.hadoop.yarn.server.nodemanager.DefaultContainerExecutor.launchContainer(DefaultContainerExecutor.java:211)\n\tat org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch.call(ContainerLaunch.java:302)\n\tat org.apache.hadoop.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch.call(ContainerLaunch.java:82)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:262)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)\n\tat java.lang.Thread.run(Thread.java:744)\n\n\nContainer exited with a non-zero exit code 255\nFailing this attempt. Failing the application.","clusterId":1481567788061,"applicationType":"SPARK","applicationTags":"","startedTime":1481568051052,"finishedTime":1481568079289,"elapsedTime":28237,"amHostHttpAddress":"sandbox:8042","allocatedMB":-1,"allocatedVCores":-1,"runningContainers":-1,"memorySeconds":55598,"vcoreSeconds":27,"preemptedResourceMB":0,"preemptedResourceVCores":0,"numNonAMContainerPreempted":0,"numAMContainerPreempted":0},{"id":"application_1481567788061_0001","user":"root","name":"pi.py","queue":"default","state":"FINISHED","finalStatus":"SUCCEEDED","progress":100.0,"trackingUI":"History","diagnostics":"","clusterId":1481567788061,"applicationType":"SPARK","applicationTags":"","startedTime":1481567853324,"finishedTime":1481567888648,"elapsedTime":35324,"amContainerLogs":"http://sandbox:8042/node/containerlogs/container_1481567788061_0001_01_000001/root","amHostHttpAddress":"sandbox:8042","allocatedMB":-1,"allocatedVCores":-1,"runningContainers":-1,"memorySeconds":138031,"vcoreSeconds":66,"preemptedResourceMB":0,"preemptedResourceVCores":0,"numNonAMContainerPreempted":0,"numAMContainerPreempted":0}]}} 

ich daraus ein List[Application] extrahieren möchte, sollte sich unter Anwendung ist:

case class Application(id: String, user: String, name: String) 

I spray-json importiert. Wenn message ist eine Zeichenfolge, die JSON-Komponente enthält, möchte ich, wie etwas tun:

val json: JsValue = message.parseJson 
    val jobsJson = json.first.first 
    val jobs = jobsJson.map(job => Application(job(0), job(1), job(2))) 

Aber das ist nicht richtig, weil ich nicht json.first verwenden können.

Wie kann ich Felder extrahieren, die im JSON-Objekt verschachtelt sind? Gibt es eine andere Bibliothek, die die Dinge einfacher macht?

+0

Welche JSON-Lib verwenden Sie? – cchantep

+0

Ich benutze 'spray-json' – octavian

Antwort

1

Sie sollten in der Lage sein, Daten zu erhalten, aus dem JSON-Objekt mit \ oder \\ Ein einzelnen Schrägstrich wird in dem nächsten Hebel nach unten schaut für was auch immer Sie suchen, während ein doppelter Schrägstrich durch das ganze Objekt aussehen wird. Sagen Sie hatte die folgende json in einer Variablen obj gespeichert genannt:

{"foo":"bar","num":3, "value":{"num":4}} 

obj\num verwenden würden Sie nur 3. bekommen aber mit obj\\num würden Sie in Iterator sowohl mit 3 und 4 in es.

versuchen Sie this link für ein wenig mehr Informationen.

+0

Kann ich die Bibliothek' play.json' verwenden, ohne 'play' zu verwenden? – octavian

+0

@octavian http://stackoverflow.com/questions/29709008/is-it-possible-to-use-json-library-without-installing-play hilft das? – Buzz

+0

Großartig! Vielen Dank! – octavian