Deserializing a JSON dictionary

2019-08-05 19:45发布

问题:

Lots of headache here, how do I deserialize JSON like the one in the image? I'd usually see things like {"Word" : "Fish","Definition" : "It is an animal", etc} but the one I have found that is the closest thing to what I am looking for does not specify the value type, hence I can't really deserialize it with case classes. I'm sorry that I'd really prefer a clear explanation, I'm bad at this.

I'm really looking forward to an answer, thank you for reading.

回答1:

It would appear that you are trying to parse an actual dictionary that is in a psuedo json format but looks more like a key value pair surrounded by braces. If I were going to solve a problem like this I would use basic string parsing. To properly deserialize JSON the JSON has to be valid and you must be able to represent the json as a case class. The text in the image can't be represented as a case class because non of the elements appear more than once.

Here is a working example of how I would solve this problem in scala.

    scala> val test = "{\"dog\":\"animal with four legs\",\"fish\":\"underwater animal\",\"horse\":\"tall running animal\"}"
//test: String = {"dog":"animal with four legs","fish":"underwater animal","horse":"tall running animal"}

scala> val test2 = test.replace("{","").replace("}","").replace("\"","")
//test2: String = dog:animal with four legs,fish:underwater animal,horse:tall running animal

scala> val test3 = test2.split(",")
//test3: Array[String] = Array(dog:animal with four legs, fish:underwater animal, horse:tall running animal)

scala> val test4 = test3.map(innerValue => innerValue.split(":"))
//test4: Array[Array[String]] = Array(Array(dog, animal with four legs), Array(fish, underwater animal), Array(horse, tall running animal))

scala> val test5 = test4.map( outerArray => outerArray(0) -> outerArray(1)).toMap
//test5: scala.collection.immutable.Map[String,String] = Map(dog -> animal with four legs, fish -> underwater animal, horse -> tall running animal)

scala> test5("dog")
//res1: String = animal with four legs

Steps:

Test: define the string as a variable

Test2: remove text that isn't needed using the replace function chained several times

Test3: Split the string into several arrays based on the commas

Test4: Iterate through the array and split smaller strings on the :

Test5: Iterate through the array of arrays and make key value pairs and then convert into a map.

Test5 is now a scala map representation of the type of document shown in the picture and you can access values based on keys.

While this will work it would be slow for a large document and it might be better to represent this as a properly defined JSON document that can be serialized and deserialized using standard methods. A properly formatted json document could look like.

{
"dictionary_entries": [
    {
        "term": "dog",
        "description": "animal with four legs"
    },
    {
        "term": "fish",
        "description": "underwater animal"
    },
    {
        "term": "horse",
        "description": "tall running animal"
    }
]
}