简体   繁体   English

如何使用scala读取嵌套的Json?

[英]How to read nested Json using scala?

I am using scala to separate json. 我正在使用scala来分离json。 I have following Json structure- 我有以下Json结构-

"commands":{
"myinfo": [
    {
        "utilization": {
            "sizeBytes": 998848331776,
            "usedBytes": 408722341888,
            "freeBytes": 590125989888
        },
        "name": "ds1",
        "addons": [
            "PQR",
            "ABC"
        ],
        "otherInfo": {
            "model": "MRSASRoMB-4i",
            "name": "naa.6d867d9c7acd60001aed76eb2c70bd53",
            "vendor": "LSI"
        }
    }
]
}}

I want to read value of otherInfo, utilization etc. I can read value of name using following code- 我想读取otherInfo的值,利用率等。我可以使用以下代码读取名称的值-

val commandInfo = (rawData \ "Commands").as[JsValue]
(commandInfo \ "myInfo").as[List[JsObject]].map { myJson =>
val name =  (myJson \  "name").asOpt[String]
}

I am using case classes in scala. 我在Scala中使用案例类。 How do I get values of 'otherInfo','addons' etc using scala? 如何使用Scala获取“ otherInfo”,“ addons”等值?

Are you using dispatch's JSON support? 您是否正在使用调度的JSON支持? If so, then you might want to consider using json4s instead, which is little like the de facto standard. 如果是这样,那么您可能要考虑使用json4s来代替,这有点像事实上的标准。

I personally would consider trying to use SON of JSON instead, which would make getting the name out a breeze: 我个人将考虑尝试使用JSON的SON ,这将使名称变得轻而易举:

commands.myinfo.otherinfo.name.as[String]

… but then again, that's a shameless plug for something I wrote myself. ……但话又说回来,那是我写给自己的东西的一个无耻的插件。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM