How to convert Scala Map into JSON String?

前端 未结 7 1809
一向
一向 2021-02-07 04:19

For example, I have this Map value in Scala:

val m = Map(
    \"name\" -> \"john doe\", 
    \"age\" -> 18, 
    \"hasChild\" -> true, 
    \"childs\" -         


        
7条回答
  •  滥情空心
    2021-02-07 04:25

    val mymap = array.map {
      case 1 => ("A", 1)
      case 2 => ("B", 2)
      case 3 => ("C", 3)
    }
      .toMap
    

    Using scala.util.parsing.json.JSONObject, you only need 1 line:

    import scala.util.parsing.json.JSONObject
    
    JSONObject(mymap).toString()
    

提交回复
热议问题