将 (String, List[(String, String)]) 转换为 JSON 对象

Convert a (String, List[(String, String)]) to JSON object

我的数据为:

(ID001,List((BookType,[text]),(author,xyz abc),(time,01/12/2019[22:00] CST/PM))),(ID002,List((BookType,[text]),(author,klj fgh),(time,19/02/2019[12:00] CST/AM)))

我需要将其转换为 JSON 对象:

{"ID001":{
    "BookType":"[text]",
    "author":"xyz abc",
    "time":"01/12/2019[22:00] CST/PM" 
    },
{"ID002": {
    "BookType":"[text]" , 
    "author":"klj fgh", 
    "time":"19/02/2019[12:00] CST/AM"
    } 
}

我是 Scala Spark 的新手。知道如何转换它。

为了创建 json 使用已经很好的 jackson utilities.In 你的情况可以使用下面的代码

      val mapper = new ObjectMapper`
      val data = ("ID01", List(("BookType", "[text]"),("author", "xyz abc"),"time","01/12/2019[22:00] CST/PM")))
      val id = mapper.createObjectNode()
      val bookDetails = mapper.createObjectNode()
      data._2.foreach(x => {bookDetails.put(x._1, x._2)})
      id.set(data._1, bookDetails)
      println(id.toString)

上面的输出结果如下

{"ID01":{"BookType":"[text]","author":"xyz abc","time":"01/12/2019[22:00] CST/PM"}}