2013-08-08 11 views
7

Mam przykładowy kod, jak poniżej.Scala Play Json czyta

import play.api.libs.json._ 
import play.api.libs.functional.syntax._ 
import play.api.data.validation.ValidationError 
import play.api.libs.json.Reads._ 

case class Retailer(firstName:String,lastName:String,email:String,mobileNo:String,password:String) 
case class Business(name:String,preferredUrl:String,businessPhone:String,retailer:Retailer) 

object JsonTest { 
    val jsonValue = """ 
    { 
    "business": 
    { 
     "name":"Some Business Name", 
     "preferredUrl":"someurl", 
     "businessPhone":"somenumber", 
     "retailer": 
     { 
     "firstName":"Some", 
     "lastName":"One", 
     "email":"[email protected]", 
     "mobileNo":"someothernumber", 
     "password":"$^^HFKH*" 
     } 
    } 

    } 
    """ 
    def printJson ={ 

    implicit val rltRds = (
     (__ \ "firstName").read[String] ~ 
     (__ \ "lastName").read[String] ~ 
     (__ \ "email").read[String] ~ 
     (__ \ "mobileNo").read[String] ~ 
     (__ \ "password").read[String] 
    )(Retailer)  

    implicit val bsnsRds = (
     (__ \ "name").read[String] ~ 
     (__ \ "preferredUrl").read[String] ~ 
     (__ \ "businessPhone").read[String] ~ 
     (__ \ "retailer").read[Retailer](rltRds) 
    )(Business)  


    val buisness = Json.parse(jsonValue).validate[Business](bsnsRds) 
    val bus = new Business("Some Business","somebusinessurl","somenumber", new Retailer("Some","One","[email protected]","someothernumber","$^^HFKH*")) 
    //val json = Json.toJson(bus) 

    println(buisness) 
    } 




    def main(args: Array[String]): Unit = { 
    printJson 
    } 

} 

otrzymuję błąd Json Validation gdy próbuję analizować JSON do obiektu Scala Business Class (w tym przypadku). Błąd jest

Jednak jeśli mój JSON jest jak

val jsonValue = """ 
    { 
     "name":"Some Business Name", 
     "preferredUrl":"someurl", 
     "businessPhone":"somenumber", 
     "retailer": 
     { 
     "firstName":"Some", 
     "lastName":"One", 
     "email":"[email protected]", 
     "mobileNo":"someothernumber", 
     "password":"$^^HFKH*" 
     } 
    } 
    """ 

nocie, że zewnętrzna wspornik „{” i „biznes” Kluczem są usuwane. Otrzymuję JsSuccess. Jak napisać odczyty dla Json jak w pierwszym przypadku? Ponadto, jak mogę to zrobić w sposób ogólny?

Proszę o pomoc.

Odpowiedz

7

Wystarczy dodać klucz business w ścieżce:

implicit val bsnsRds = (
     (__ \ "business" \ "name").read[String] ~ 
     (__ \ "business" \ "preferredUrl").read[String] ~ 
     (__ \ "business" \ "businessPhone").read[String] ~ 
     (__ \ "business" \ "retailer").read[Retailer](rltRds) 
    )(Business) 
+0

to działa. Dzięki nico_ekito. – user809564

+0

Ok, ale czy istnieje prosty sposób przejścia z pierwszej formy do drugiej, aby użyć niejawnego val businessReads = Json.reads [Business]? – Simon

+0

Ho przepraszam, znalazłem rozwiązanie, możesz zrobić jsonValue \ "business" – Simon

1

niewielkie zmiany do powyższego:

implicit val bsnsRds = ({ 
    val business = (__ \ "business") 
    (business \ "name").read[String] ~ 
    (business \ "preferredUrl").read[String] ~ 
    (business \ "businessPhone").read[String] ~ 
    (business \ "retailer").read[Retailer](rltRds) 
})(Business) 
Powiązane problemy