json - How do I avoid nested Array when serializing a Map with Play Writes? -


I am trying to serialize the map using the Json library from Play. I wrote my own written because there is no one to map.

  Import play.api.libs.json.Json._ import play.api.libs.json._Object Nestedere app {val M: map [int, string] = map (1 - & gt; "one", 2 - & gt; "two") Built-in values ​​mWrites = new writing [map [int, string]] {def writes (m: map [int, string]) = arr (m. Keys.map (k = & gt; {obj ("key" -> gt.toString, "value" -> m (k)}}))} val j = toson [map [int, string]] ( M) PrintLine (beautiful print (j))}  

The output is:

  [[{"key": "1", "value": print) "One"}, {"key": "2", "value": "two"}]]  

as you give Can have two pairs of the [] around objects when I use the wrapper classes around the map, so I only get one pair []. Wrap the case class (m: map [int, string]) val w = new wrap (m) inherent val rap wars = new litte [wrap] {writes df (w: rape) = Obizage ("m" -> wmkeys.map (k = & gt; {oBJ ("key" -> gt; ktostring, "value"

output: < / P>

     

Code "{" m ": [{" key ":" 1 "," value ":" one "}

Div>

Json.arr its logic list Creates a JSON array because the first argument itself is a sequence, the result is a sequence of sequence.

For example.

  scala> Jason .RR (1 , 2,3) Race 1: Play.API.Lib Jason.JSERE = [1,2,3] Scala> Jason .RR (List (1,2,3)) res2: play.api.libs. Json.jsArray = [[1,2,3]]  

changing the call to AR and directly to Jason, the nested array is removed

  Import play.api.libs.json.Json._ import play.api.libs.json ._Object Nested A Scope app {val m: map [Int, String] = map (1 - & gt; "One", 2 - & gt; "Two") underlying values ​​mWrites = new writing [map [int, string]] {def writes (M: map [int, string]): jsvl = jason.tiesson (mkis map (k = c {OBJ ("key" -> K.oststring, "value" -> m (k)}})} val j = tosonson [map [int, string]] (m) printlen (beautiful print (j ))}  

Comments