反序列化JSON字典 [英] Deserializing a JSON dictionary

查看:145
本文介绍了反序列化JSON字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里令人头疼,我如何像图像中那样反序列化JSON?我通常会看到类似{"Word":"Fish","Definition":"It is a animal"等}之类的东西,但是我发现与我所寻找的东西最接近的东西并没有指定值类型,因此我无法真正使用案例类对它进行反序列化.很抱歉我真的希望有一个清晰的解释,我对此很不好.

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.

推荐答案

您似乎正在尝试解析psuedo json格式的实际字典,但看起来更像是用括号括起来的键值对.如果我要解决这样的问题,我将使用基本的字符串解析.为了正确地反序列化JSON,JSON必须有效,并且您必须能够将JSON表示为case类.图片中的文本不能表示为case类,因为没有一个元素出现多次.

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.

这是我如何在Scala中解决此问题的有效示例.

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

步骤:

测试:将字符串定义为变量

Test: define the string as a variable

Test2:使用链接数次的替换功能删除不需要的文本

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

Test3:根据逗号将字符串分成几个数组

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

Test4:遍历数组,并在:上拆分较小的字符串:

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

Test5:遍历数组数组并创建键值对,然后转换为映射.

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

Test5现在是图片中显示的文档类型的scala映射表示形式,您可以基于键访问值.

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

虽然这将起作用,但对于大型文档而言会比较慢,最好将其表示为可以使用标准方法进行序列化和反序列化的正确定义的JSON文档.格式正确的json文档可能看起来像这样.

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"
    }
]
}

这篇关于反序列化JSON字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆