Scala Convert a string into a map

2019-03-30 07:45发布

问题:

What is the fastest way to convert this

{"a":"ab","b":"cd","c":"cd","d":"de","e":"ef","f":"fg"}

into mutable map in scala ? I read this input string from ~500MB file. That is the reason I'm concerned about speed.

回答1:

That looks like a JSON file, as Andrey says. You should consider this answer. It gives some example Scala code. Also, this answer gives some different JSON libraries and their relative merits.



回答2:

If your JSON is as simple as in your example, i.e. a sequence of key/value pairs, where each value is a string. You can do in plain Scala :

myString.substring(1, myString.length - 1)
        .split(",")
        .map(_.split(":"))
        .map { case Array(k, v) => (k.substring(1, k.length-1), v.substring(1, v.length-1))}
        .toMap


回答3:

The fastest way to read tree data structures in XML or JSON is by applying streaming API: Jackson Streaming API To Read And Write JSON.

Streaming would split your input into tokens like 'beginning of an object' or 'beginning of an array' and you would need to build a parser for these token, which in some cases is not a trivial task.



标签: json scala map