1

我正在尝试使用 flexjson 反序列化从网络调用中获得的字符串。问题是其中的一些元素在属性/键中有一个点,例如:

[{... 
       "contact.name": "Erik Svensson", 
       "contact.mail": "erik.svensson@foo.bar",
       "contact.phone": "0731123243", 
...}]

现在,除了这些带点的字符串之外,其他所有内容都已到位,它们最终在我的目标类中为空。我猜这是因为它不知道将它们映射到什么,因为我无法在我的容器类中声明一个带有点的变量。

这是我现在要反序列化的代码,

mData = new JSONDeserializer<List<Thing>>()
  .use("values", Thing.class)
  .deserialize(reader);

如何修改它以捕获带有点的字符串并将它们放在我的 Things 类中:

String contactName; 
String contactMail;
String contactPhone;

// getters&setters

注意我对序列化没有任何控制权..

4

2 回答 2

0

我也很好奇,如果这种类型的分配可以很容易地完成,我已经玩过一些代码,这就是我想出的。(我在这里发布它是因为它可能对有一些相关问题的人有所帮助,或者只是作为一个起点。)

PrefixedObjectFactory见下文)将从 JSON 对象的字段名称中截取固定前缀,并使用此名称查找匹配的 bean 属性。可以轻松更改代码以进行替换(例如,将 a 之后的第一个字母设置.为大写并删除.

它可以这样使用:

List<Thing> l = new JSONDeserializer<List<Thing>>().use("values", new PrefixedObjectFactory(Thing.class, "contact.")).deserialize(source);

编码:

import flexjson.ObjectBinder;
import flexjson.ObjectFactory;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Type;
import java.util.Map;

public class PrefixedObjectFactory<T> implements ObjectFactory {

    protected Class<T> clazz;
    protected String prefix;

    public PrefixedObjectFactory(Class<T> c, String prefix) {
        this.clazz = c;
        this.prefix = (prefix == null) ? "" : prefix;
    }

    @Override
    public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) {
        try {
            Class useClass = this.clazz;
            T obj = (T)useClass.newInstance();
            if (value instanceof Map) {
                // assume that the value is provided as a map
                Map m = (Map)value;
                for (Object entry : m.entrySet()) {
                    String propName = (String)((Map.Entry)entry).getKey();
                    Object propValue = ((Map.Entry)entry).getValue();
                    propName = fixPropertyName(propName);
                    propValue = fixPropertyValue(propValue);
                    assignValueToProperty(useClass, obj, propName, propValue);
                }
            } else {
                // TODO (left out here, to keep the code simple)
                return null;
            }
            return obj;
        } catch (Exception ex) {
            return null;
        }
    }

    protected String fixPropertyName(String propName) {
        if (propName.startsWith(this.prefix)) {
            propName = propName.substring(this.prefix.length());
        }
        return propName;
    }

    protected Object fixPropertyValue(Object propValue) {
        return propValue;
    }

    protected PropertyDescriptor findPropertyDescriptor(String propName, Class clazz) {
        try {
            return new PropertyDescriptor(propName, clazz);
        } catch (Exception ex) {
            return null;
        }
    }

    protected void assignValueToProperty(Class clazz, Object obj, String propName, Object propValue) {
        try {
            PropertyDescriptor propDesc = findPropertyDescriptor(propName, clazz);
            if (propDesc != null) {
                propDesc.getWriteMethod().invoke(obj, propValue);
            }
        } catch (Exception ex) {
        }
    }
}
于 2014-01-03T16:16:47.250 回答
0

好的所以我已经解决了这个问题,但我不得不放弃 flexJson。找遍了整个地方寻找一种简单的方法,但找不到一个。

相反,我和杰克逊一起去了,这就是我最终得到的结果:

ObjectMapper mapper = new ObjectMapper();
mThings = mapper.readValue(url, new TypeReference<List<Thing>>() {});

在我的课堂上:

@JsonProperty("contact.name")
private String contactName;

@JsonProperty("contact.mail")
private String contactMail;

@JsonProperty("contact.phone")
private String contactPhone;

// getters and setters..

如果有人知道如何使用 FlexJson 做到这一点,请随时发布答案,我想看看。

于 2013-12-12T12:18:47.290 回答