2013-02-01 15 views
16

Yürütme'de bir JSON değerindeki bir değeri nasıl değiştiririm?
kodu göstermek için: çizgisinde
Yürütme'de bir JSON değeri nasıl değiştirilir

def newReport() = Action(parse.json) { request => 
    var json = request.body 
    if((json \ "customerId").as[Int] == -1){ 
     // replace customerId after some logic to find the new value 
    } 
    json.validate[Report](Reports.readsWithoutUser).map { 
     case _: Report => 
+4

. Bu yol aşağı çılgınlık yatıyor. json dizesini yerel bir veri yapısına dönüştürür, yapıyı değiştirir, daha sonra json'a yeniden kodlarsınız. –

+0

MarcB gibi ya da Json Coast2Coast özelliklerini kullanın http://mandubian.com/2012/10/29/unveiling-play-2-dot-1-json-api-part3-json-transformers/ –

+1

[Cevabım] (http://stackoverflow.com/a/18069519/2643828) sizin için yararlı oldu, lütfen kabul edilen yanıt olarak seçin. – Zeimyth

cevap

2

bir parça:

val updatedJson = if((request.body \ "customerId").as[Int] == -1){ 
    val newId = JsObject(Seq(("customerId",JsString("ID12345")))) 
    (request.body ++ newId).as[JsValue] 
} else request.body 

updatedJson.validate[Report](Reports.readsWithoutUser).map { 
    case _: Report => 
+0

Yürütme Json veri yapıları değişmez olduğu için değeri güncelleyemezsiniz, ancak güncellenmiş alan haricinde aynı olan yeni bir JsValue oluşturabilirsiniz. – scalapeno

25

Play Documentation göre, JsObjects iki JsObjects birleştirme bir yöntem ++ sahiptir. Eğer yeni tamsayı değeri, yani, sadece gerekir:

val updatedJson = json.as[JsObject] ++ Json.obj("customerId" -> newValue) 

Çal 2.1.x itibariyle sen + kullanabilirsiniz:

val updatedJson = json.as[JsObject] + ("customerId" -> newValue) 
1

uzakta değişmez olanların hepsi hareketli düşünüyorum "JSON" çözümleri. Bu sadece kodu korkunç bir karmaşa haline getiriyor. Bu SON of JSON içinde nasıl görüneceğini geçerli:

import nl.typeset.sonofjson._ 

val json = … 
if (json.customerId.as[Int] == -1) { 
    json.customerId = 987938 
} 
2

Bir yaklaşım olduğunu, Marc B söylediği gibi, bu işlemek, bir vaka sınıf gibi bir şey JSON dönüştürmek ve sonra yeni bir JSON oluşturun. Ancak JSON 'transformatörlerini de kullanabilirsiniz. Etkili bir şekilde yaptığınız bir Reads [SomeThing] nesnesi oluşturmaktır. Bu nesne, JSON nesnesinde çağırdığınız dönüştürme yöntemine iletilir. JSON nesnesini değiştirecek ve sonucun yeni değiştirilmiş JSON olduğu bir Başarı (sonuç) veya Hata (hata) döndürecektir. formatının json kullanarak

: İşte (nispeten) çok basit bir örnek {anahtar -> value}

def jsonXForm(value: String) = (__ \ "customerId").json.update(
    (__ \ "customerId").json.put(JsString(value)) 
) 
json.transform(jsonXForm(yourNewValue)) match {...}` 

Zeimyth en bir iyi kılavuzu here

0

bir yüceltilmiş bir sürümü var yanıtı örtülü olarak kullanır dönüşüm

implicit class JsObjectEnhancer(jsObject: JsObject) { 
    def update(args: (String, Json.JsValueWrapper)*): JsObject = { 
    jsObject ++ Json.obj(args: _*) 
    } 
} 

kullanım (json tipi JSObject olmalı ve örtük sınıf kapsamında olmalı) Eğer json dizeleri kullanmayın.Her

json.update("customerId", 1000) 
İlgili konular