Java json字符串以未知数对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21646212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:51:06  来源:igfitidea点击:

json string to object with unknowns

javajsonstring

提问by ViRALiC

As the title describes I'm trying to assign a json string to my class, but the json string will presumably contain several variables which I have not created in the class they are being assigned to.

正如标题所描述的,我正在尝试为我的类分配一个 json 字符串,但是 json 字符串可能包含几个我没有在它们被分配到的类中创建的变量。

To show you first what works, I have

为了首先向您展示什么有效,我有

    ObjectMapper mapper = new ObjectMapper();

    try {

        Tweet tweet1 = mapper
                .readValue(
                        "{\"created_at\":\"Sat Feb 08 11:26:02 +0000 2014\"}",
                        Tweet.class);

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

That works, as the class, Tweet, has the variable "created_at" with getter and setter methods all in place.

这是有效的,因为 Tweet 类具有变量“created_at”,并带有 getter 和 setter 方法。

Like mentioned though, I need to expect the string in the readValue method to contain things which are not to be found in my Tweet class.

就像提到的那样,我需要期望 readValue 方法中的字符串包含在我的 Tweet 类中找不到的东西。

To get around this, I import and use:

为了解决这个问题,我导入并使用:

    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Which theoretically helps me avoid all the unknowns in there, and just assigns the values that are to be found in the Tweet class.

从理论上讲,这可以帮助我避免那里的所有未知数,并且只分配要在 Tweet 类中找到的值。

So, I add a value which is not to be found in Tweet, like so:

因此,我添加了一个在 Tweet 中找不到的值,如下所示:

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    try {

        Tweet tweet1 = mapper
                .readValue(
                        "{\"created_at\":\"Sat Feb 08 11:26:02 +0000 2014\",\"id\":432113085571407872\"}",
                        Tweet.class);

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

Now, the value "id" does not exist in my Tweet class, and should therefor be ignored (no?).

现在,值“id”在我的 Tweet 类中不存在,因此应该被忽略(不是吗?)。

But, now the console throws me an error:

但是,现在控制台给我一个错误:

com.fasterxml.Hymanson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [Source: java.io.StringReader@7817ea54; line: 1, column: 72]

com.fasterxml.Hymanson.core.JsonParseException: Unexpected character ('"' (code 34)): 期望用逗号分隔 [Source: java.io.StringReader@7817ea54; line: 1, column: 72]

Now this makes me a sad panda, because I cannot for the life of me understand what it is I'm doing wrong.

现在这让我成为一个悲伤的熊猫,因为我一辈子都无法理解我做错了什么。

Anyone with better eyes and/or a sharper mind than mine, see my mistake?

任何比我眼睛更好和/或头脑更敏锐的人,看到我的错误了吗?

Or am I going about this all wrong?

或者我在这一切都错了吗?

采纳答案by JB Nizet

Your JSON is invalid. Here's the value of the id attribute:

您的 JSON 无效。这是 id 属性的值:

432113085571407872\"

You see that either it has a quote missing at the beginning, or it has a quote that shouldn't be there at the end.

您会看到它的开头缺少引号,或者结尾处不应该出现引号。

That's what the error message says, BTW:

这就是错误消息所说的,顺便说一句:

Unexpected character ('"'

意外字符 ('"'