简体   繁体   English

如何在Scala中将复杂JSON字符串转换为MAP

[英]How to convert Complex JSON string in to MAP in scala

I have a text file which contains a line like 我有一个文本文件,其中包含一行

players={"Messi":{"Details":{"Goals":500},"Country":"Argentina"},"Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"], "Country":"Brazil"}} 球员= {“梅西”:{“详情”:{“目标”:500},“国家”:“阿根廷”},“内马尔”:{“俱乐部”:[“桑托斯”,“巴塞罗那足球俱乐部”,“巴黎” saint German“],”Country“:”Brazil“}}

Now I am used a regex for extract the 现在我用一个正则表达式来提取

{"Messi":{"Details":{"Goals":500},"Country":"Argentina"},"Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"],"Country":"Brazil"}} {“Messi”:{“Details”:{“Goals”:500},“Country”:“Argentina”},“Neymar”:{“Clubs”:[“Santos”,“FC barcelona”,“Paris saint German” “],”国家“:”巴西“}}

from the text file and pass it in to a case class which accepts the value as a String . 从文本文件中传入并传递给一个case类,该类接受值作为String

and I am making a Dataframe using this case class. 我正在使用这个案例类创建一个Dataframe。

In my case every line may be different in the contents with in the JSON String.So I am looking for a general solution to Convert any complex Json string to Map values. 在我的情况下,每个行的内容可能与JSON字符串不同。所以我正在寻找将任何复杂的Json字符串转换为Map值的通用解决方案。

When checking dataframe.printSchema , I am getting the players column as a String type. 检查dataframe.printSchema时 ,我将player列作为String类型。 But I need it to be as a Map type which holds a Key and value as a Struct type. 但我需要它作为一个Map类型,它包含一个Key和值作为Struct类型。 I tried method referred in this link 我试过这个链接中提到的方法

How can I convert a json string to a scala map? 如何将json字符串转换为scala映射?

when I used this way,I got error 当我用这种方式时,我得到了错误

"org.json4s.package$MappingException: Do not know how to convert JObject(List((Details,JObject(List((Goals,JString(500))))), (Country,JString(Argentina)))) into class java.lang.String " “org.json4s.package $ MappingException:不知道如何转换JObject(List((Details,JObject(List((Goals,JString(500))))),(Country,JString(Argentina))))到类java.lang.String“

and I used following solutions 我使用了以下解决方案

Converting JSON string to a JSON object in Scala 在Scala中将JSON字符串转换为JSON对象

But these too won't worked for me. 但这些也不会对我有用。

This is my case class 这是我的案例类

case class caseClass (
                       Players :String = ""
                     )

I am Extracting the json string using a user defined function. 我正在使用用户定义的函数提取json字符串。

Simply my requirement is that I have a complex Json String which contains keys and values as struct,list etc.. 我的要求就是我有一个复杂的Json String,其中包含键和值作为struct,list等。

so I want to make the string to its corresponding JSON which holds a proper schema with respect to its contents. 所以我想将字符串设置为相应的JSON,该JSON包含与其内容相关的正确模式。

Kindly expecting Valuable solutions. 请期待有价值的解决方案。

If you also can live with JsValue as value instead of String it looks a bit simpler: 如果您也可以使用JsValue作为值而不是String那么它看起来会更简单:

import play.api.libs.json._

case class CaseClass (
           Players :Option[JsValue]
         )

object CaseClass {

  implicit val jsonFormat = Json.format[CaseClass ]
}

I saw some problems with your Json - so you would need to have something like: 我看到你的Json有些问题 - 所以你需要有类似的东西:

val json = Json.parse("""{
  "Players":{
     "Messi":{"Details":{"Goals":500},"Country":"Argentina"},
     "Neymar":{"Clubs":["Santos", "FC barcelona", "Paris saint German"], "Country":"Brazil"}
  }
}"""
  )

To get a String out of this you can use: 要从中获取字符串,您可以使用:

  json.validate[CaseClass] match {
    case JsSuccess(cc, _) => cc.Players.toString
    case JsError(errors) => // handle errors
  }  

I got another solution which I think More easier. 我有另一种解决方案,我认为更容易。

I Made an own schema for the JSON and Used from_json method with the schema,and it worked well. 我为JSON创建了一个自己的模式,并使用了from_json方法和模式,它运行良好。

from_json(col("Players"),ownschema).as("new_Json")

and my ownschema contains the structure of the Json String. 我的ownschema包含Json String的结构。

For any doubts, Comment. 对于任何疑惑,评论。

Happy Coding. 快乐的编码。

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

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