Scala Json 解析

发布于 2025-01-18 23:35:03 字数 3627 浏览 2 评论 0原文

这是我得到的输入 json,它是嵌套的 json 结构,我不想直接映射到类,需要自定义解析一些对象,因为我已经制作了案例类我

{
    "uuid": "b547e13e-b32d-11ec-b909-0242ac120002",
    "log": {
        "Response": {
            "info": {
                "receivedTime": "2022-02-09T00:30:00Z",
                "isSecure": "Yes",

                "Data": [{
                    "id": "75641",
                    "type": "vendor",
                    "sourceId": "3",
                    "size": 53
                }],
                "Group": [{
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner1",
                            "compressionType": "gz",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "1"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner12",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "2"
                    },
                    {
                        "isActive": "yes",

                        "metadata": {
                            "owner": "owner123",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner124",
                            "compressionType": "snappy",
                            "comments": "someComments",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    }
                ]
            }
        }
    }
}

正在尝试播放 json 的代码也尝试了 circe 。请帮忙..下面的scala世界

是对象和案例类的新内容

case class DataCatalog(uuid: String, data: Seq[Data], metadata: Seq[Metadata])

object DataCatalog {
case class Data(id: String,
                       type: String,
                       sourceId: Option[Int],
                       size: Int)

case class Metadata(isActive: String,
                    owner: String,
                    compressionType: String,
                    comments: String,
                    createdDate: String,
                    updatedDate: String
                    )


 def convertJson(inputjsonLine: String): Option[DataCatalog] = {
    val result = Try {
      //val doc: Json = parse(line).getOrElse(Json.Null)
      //val cursor: HCursor = doc.hcursor
      //val uuid: Decoder.Result[String] = cursor.downField("uuid").as[String]
      
      val lat = (inputjsonLine \ "uuid").get

      
     
      DataCatalog(uuid, data, group)
    }
    //do pattern matching
    result match {
      case Success(dataCatalog) => Some(dataCatalog)
      case Failure(exception) =>
        
    }
  }
  }

任何解析api都可以。

This is the input json which I am getting which is nested json structure and I don't want to map directly to class, need custom parsing of some the objects as I have made the case classes

{
    "uuid": "b547e13e-b32d-11ec-b909-0242ac120002",
    "log": {
        "Response": {
            "info": {
                "receivedTime": "2022-02-09T00:30:00Z",
                "isSecure": "Yes",

                "Data": [{
                    "id": "75641",
                    "type": "vendor",
                    "sourceId": "3",
                    "size": 53
                }],
                "Group": [{
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner1",
                            "compressionType": "gz",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "1"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner12",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "2"
                    },
                    {
                        "isActive": "yes",

                        "metadata": {
                            "owner": "owner123",
                            "compressionType": "snappy",
                            "comments": "someComment",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    },
                    {
                        "isActive": "yes",
                        "metadata": {
                            "owner": "owner124",
                            "compressionType": "snappy",
                            "comments": "someComments",
                            "createdDate": "2022-01-11T11:00:00Z",
                            "updatedDate": "2022-01-12T14:17:55Z"
                        },
                        "setId": "4"
                    }
                ]
            }
        }
    }
}

Code that I am trying play json also tried circe . Please help ..New to scala world

below is object and case class

case class DataCatalog(uuid: String, data: Seq[Data], metadata: Seq[Metadata])

object DataCatalog {
case class Data(id: String,
                       type: String,
                       sourceId: Option[Int],
                       size: Int)

case class Metadata(isActive: String,
                    owner: String,
                    compressionType: String,
                    comments: String,
                    createdDate: String,
                    updatedDate: String
                    )


 def convertJson(inputjsonLine: String): Option[DataCatalog] = {
    val result = Try {
      //val doc: Json = parse(line).getOrElse(Json.Null)
      //val cursor: HCursor = doc.hcursor
      //val uuid: Decoder.Result[String] = cursor.downField("uuid").as[String]
      
      val lat = (inputjsonLine \ "uuid").get

      
     
      DataCatalog(uuid, data, group)
    }
    //do pattern matching
    result match {
      case Success(dataCatalog) => Some(dataCatalog)
      case Failure(exception) =>
        
    }
  }
  }

Any parsing api is fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2025-01-25 23:35:03

如果您使用Scala Play,则对于每个情况类别类别,您应该有一个伴随对象,它将在JSON中/从JSON中进行读/写对象有很多帮助:

object Data {
  import play.api.libs.json._

  implicit val read = Json.reads[Data ]
  implicit val write = Json.writes[Data ]

  def tupled = (Data.apply _).tupled
}

object Metadata {
  import play.api.libs.json._

  implicit val read = Json.reads[Metadata ]
  implicit val write = Json.writes[Metadata ]

  def tupled = (Metadata.apply _).tupled
}

每个伴随对象都必须与案例类中的同一文件中。在JSON示例中,您需要更多的案例类,因为您那里有很多嵌套对象(日志,响应,信息,每个),

或者,您可以读取对AS的感兴趣的字段:

(jsondata \“ fieldName”)。作为[caseclassname]

您可以尝试访问数据值:

(jsonData \ "log" \ "Response" \ "info" \ "Data").as[Data]

对于元数据

If you use Scala Play, for each case class you should have an companion object which will help you a lot with read/write object in/from json:

object Data {
  import play.api.libs.json._

  implicit val read = Json.reads[Data ]
  implicit val write = Json.writes[Data ]

  def tupled = (Data.apply _).tupled
}

object Metadata {
  import play.api.libs.json._

  implicit val read = Json.reads[Metadata ]
  implicit val write = Json.writes[Metadata ]

  def tupled = (Metadata.apply _).tupled
}

Is required as each companion object to be in same file as the case class. For your json example, you need more case classes because you have a lot of nested objects there (log, Response, info, each of it)

or, you can read the field which you're interested in as:

(jsonData \ "fieldName").as[CaseClassName]

You can try to access the Data value as:

(jsonData \ "log" \ "Response" \ "info" \ "Data").as[Data]

same for Metadata

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文