2013-12-11 1 views
0

flexjson을 사용하여 웹 호출에서 문자열을 비 직렬화하려고합니다. 문제는 거기에 몇 가지 요소가 예를 들어 부동산/키 점 가지고있다 :flexjson은 도트로 속성 문자열을 deserialize합니다. " inside

[{... 
     "contact.name": "Erik Svensson", 
     "contact.mail": "[email protected]", 
     "contact.phone": "0731123243", 
...}] 

이제 다른 모든 점과 이러한 문자열을 제외한 곳에서 폭포, 그들은 내 대상 클래스에 널 (null) 끝. 그것은 점이있는 컨테이너 클래스에서 변수를 선언 할 수 없으므로 그 변수를 매핑 할 항목을 알지 못하기 때문에 그럴 것입니다.

String contactName; 
String contactMail; 
String contactPhone; 

// getters&setters 
:

이것은 내가 지금 역 직렬화하는 runnign있어 코드, 나는 점이있는 문자열을 잡아 나의 것 클래스에 넣어이를 수정하려면 어떻게

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

입니다

참고 직렬화를 제어 할 수있는 권한이 없습니다.

답변

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이 작업을 수행하는 것이 부담없이 방법을 알고있는 경우

답변을 게시, 그것을보고 싶습니다.

0

내가 궁금한 점은 이러한 유형의 과제를 쉽게 수행 할 수 있다면 몇 가지 코드로 게임을 해봤는데 이것이 내가 생각해 낸 것입니다. (여기에 게시하는 것은 관련된 질문이 있거나 누군가에게 도움이되기 때문입니다.)

PrefixedObjectFactory (아래 참조)은 JSON 객체의 필드 이름에서 고정 된 접두사를 잘라냅니다 이 이름을 사용하여 일치하는 bean 특성을 찾습니다.

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) { 
     } 
    } 
} 
관련 문제