将 JSON 反序列化为现有对象 (Java)

Posted

技术标签:

【中文标题】将 JSON 反序列化为现有对象 (Java)【英文标题】:Deserialize JSON into existing object (Java) 【发布时间】:2012-09-13 04:09:20 【问题描述】:

我想知道如何让 Jackson JSON 库将 JSON 反序列化为现有对象?我试图找到如何做到这一点;但它似乎只能接受一个类并自己实例化它。

或者如果不可能,我想知道是否有任何 Java JSON 反序列化库可以做到这一点。

这似乎是 C# 的相应问题:Overlay data from JSON string to existing object instance。 JSON.NET 似乎有一个 PopulateObject(string,object)。

【问题讨论】:

你看过 Gson 吗? code.google.com/p/google-gson 只是粗略的。能做到以上几点吗? 好像不行,code.google.com/p/google-gson/issues/detail?id=431有一个Gson问题“问题431:填充现有对象” ...而“Gson 路线图”只有“Planned Releases: Gson 2.2.3:”和空白空间。 看这个办法***.com/questions/55426453/… 【参考方案1】:

您可以使用我的存储库:)。

Object yo = //yourObject
String js = //json source

Map remote = Object$.remoteMap(yo, false); //or you can use Bean.forInstance(yo);
Reader reader = new StringReader(js);//you can replace this with any reader :)
AtomicReference buffer = new AtomicReference(remote);

try 
    JSON.global.parse(buffer, reader, null, null);
 catch (IOException ignored) 
    //If any exception got thrown by the reader

这样,JSON 会将值解析到它在缓冲区中找到的映射。如果地图包含一个列表,并且 JSON 值也有一个列表。地图上的列表不会被替换。相反,它将用于包含值。

如果你使用Bean.forInstance(yo),返回的远程地图会有一些附加功能。

存储库:

util repo(必需):github.com/cufyorg/util

基础仓库(必需):github.com/cufyorg/base

JSON 存储库(必需):github.com/cufyorg/json

beans repo(可选):github.com/cufyorg/beans

【讨论】:

【参考方案2】:

如果您使用的是 spring 框架,您可以使用 BeanUtils 库来完成此任务。首先正常反序列化您的 json 字符串,然后使用 BeanUtils 将此对象设置在父对象中。它还期望在父对象内设置对象的变量名。 这是代码sn-p:

childObject = gson.fromJson("your json string",class.forName(argType))
BeanUtils.setProperty(mainObject, "childObjectName", childObject);

【讨论】:

谢谢。恐怕这并没有真正达到我的意图。这看起来像“手动”设置字段/属性。【参考方案3】:

总是可以加载到一个虚拟对象中并使用反射来传输数据。如果你一心只想使用 gson

例子。 假设此代码在您要将数据复制到的对象中

    public void loadObject()
Gson gson = new Gson();
//make temp object
YourObject tempStorage = (YourObject) gson.fromJson(new FileReader(theJsonFile), YourObject.class);
//get the fields for that class
ArrayList<Field> tempFields = new ArrayList<Field>();
ArrayList<Field> ourFields = new ArrayList<Field>();
getAllFields(tempFields, tempStorage.getClass());
getAllFields(thisObjectsFields, this.getClass());
for(Field f1 : tempFields)
    for(Field f2 : thisObjectsFields)
        //find matching fields
        if(f1.getName().equals(f2.getName()) && f1.getType().equals(f2.getType()))
            //transient and statics dont get serialized and deserialized.
            if(!Modifier.isTransient(f1.getModifiers())&&!Modifier.isStatic(f1.getModifiers()))
                //make sure its a loadable thing
                f2.set(this, f1.get(tempStorage));
            
        
    

public static List<Field> getAllFields(List<Field> fields, Class<?> type) 
    for (Field field : type.getDeclaredFields()) 
        fields.add(field);
    
    if (type.getSuperclass() != null) 
        fields = getAllFields(fields, type.getSuperclass());
    
    return fields;

【讨论】:

【参考方案4】:

如果您可以使用其他库而不是 Jackson,您可以尝试 Genson http://owlike.github.io/genson/。除了其他一些不错的功能(例如使用不带任何注释的非空构造函数进行反序列化、反序列化为多态类型等)之外,它还支持将 JavaBean 反序列化为现有实例。这是一个例子:

BeanDescriptorProvider provider = new Genson().getBeanDescriptorFactory();
BeanDescriptor<MyClass> descriptor = provider.provide(MyClass.class, genson);
ObjectReader reader = new JsonReader(jsonString);
MyClass existingObject = descriptor.deserialize(existingObject, reader, new Context(genson));

如果您有任何问题,请随时使用其邮件列表http://groups.google.com/group/genson。

【讨论】:

谢谢,我一定会看看的!这是他们的第一个目标,看起来很有希望:Be as much extensible as possible by allowing users to add new functionnalities in a clean and easy way. Genson applies the philosophy that "We can not think of every use case, so give to users the ability to do it by them self in a easy way". 不错,希望大家喜欢,其实我是gensons作者=)【参考方案5】:

您可以使用 Jackson 来做到这一点:

mapper.readerForUpdating(object).readValue(json);

另见Merging Two JSON Documents Using Jackson

【讨论】:

谢谢,@boberj - 奇怪的是以前没有人能找到这个;该功能在我问的时候一定是在杰克逊,因为你的链接是一个描述这个的旧答案。 但不幸的是,在这种情况下,readValue 如果无法解析 json,则不会抛出异常。是否有可能将其恢复为正常行为?【参考方案6】:

我使用 Jackson + Spring 的 DataBinder 来完成这样的事情。此代码处理数组,但不处理嵌套对象。

private void bindJSONToObject(Object obj, String json) throws IOException, JsonProcessingException 
    MutablePropertyValues mpv = new MutablePropertyValues();
    JsonNode rootNode = new ObjectMapper().readTree(json);
    for (Iterator<Entry<String, JsonNode>> iter = rootNode.getFields(); iter.hasNext(); ) 
        Entry<String, JsonNode> entry = iter.next();
        String name = entry.getKey();
        JsonNode node = entry.getValue();
        if (node.isArray()) 
            List<String> values = new ArrayList<String>();
            for (JsonNode elem : node) 
                values.add(elem.getTextValue());
            
            mpv.addPropertyValue(name, values);
            if (logger.isDebugEnabled()) 
                logger.debug(name + "=" + ArrayUtils.toString(values));
            
        
        else 
            mpv.addPropertyValue(name, node.getTextValue());
            if (logger.isDebugEnabled()) 
                logger.debug(name + "=" + node.getTextValue());
            
         
    
    DataBinder dataBinder = new DataBinder(obj);
    dataBinder.bind(mpv);

【讨论】:

【参考方案7】:

flexJson 也可以帮助您做到这一点。

这是从FlexJson Doc复制的示例

deserializeInto 函数获取您的字符串并引用现有对象。

 Person charlie = new Person("Charlie", "Hubbard", cal.getTime(), home, work );
 Person charlieClone = new Person( "Chauncy", "Beauregard", null, null, null );
 Phone fakePhone = new Phone( PhoneNumberType.MOBILE, "303 555 1234");
 charlieClone.getPhones().add( fakePhone ); 
 String json = new JSONSerializer().include("hobbies").exclude("firstname", "lastname").serialize( charlie ); 
 Person p = new JSONDeserializer<Person>().deserializeInto(json, charlieClone);

请注意,p 中返回的引用与 charlieClone 相同,只是更新了值。

【讨论】:

【参考方案8】:

一种解决方案是解析一个新的对象图/树,然后统一复制到现有的对象图/树中。但这当然效率更低,工作量更大,尤其是在具体类型因类型信息可用性较低而不同的情况下。 (所以不是真正的答案。希望有更好的答案,只是想避免别人这样回答。)

【讨论】:

以上是关于将 JSON 反序列化为现有对象 (Java)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Jackson 将原始 JSON 反序列化为 Java 对象

如何将java对象序列化/反序列化为javax.JSON

使用 GSON 将 JSON 反序列化为 Java 对象时遇到问题

将Json字符串反序列化为对象java

将嵌套的 JSON 字符串反序列化为 Java 对象

使用Jackson将JSON数组反序列化为单个Java对象