1

ジャクソンライブラリを使用してJsonAPIコールバックをカスタムオブジェクトに変換するマッピングの問題があります。

Userカスタムクラスがあります:

public class User {
    protected String id;
    protected GregorianCalendar registredAt;
    protected String username;
    protected Team team;

    public User() {

    }

    public User(String id, GregorianCalendar registredAt, String username, Team team) {
        this.id = id;
        this.registredAt = registredAt;
        this.username = username;
        this.team = team;
    }

    public User(HashMap<String, Object> attributes) {
        this.id = (String) attributes.get("id");
        this.registredAt = (GregorianCalendar) attributes.get("registredAt");
        this.username = (String) attributes.get("username");
        this.team = (Team) attributes.get("team");
    }
}

これが私のコールバックです:

{
    id: "4feab37c5600bbd56f000132",
    registredAt: {
        date: "2012-06-27 09:17:15",
        timezone_type: 3,
        timezone: "Europe/Paris"
    },
    username: "John Doe",
    team: {
        id: "4feab37c5600bbd56f000130",
        indice: 1,
        name: "John Doe's Team",
        score: 200
    }
}

次に、Userオブジェクトを初期化しようとします。

// Code from a doInBackground() method of a custom AsyncTask.
URL completePath = new URL(apiBaseURL + "api/user/4feab37c5600bbd56f000132/show");
APIconnection = (HttpURLConnection) completePath.openConnection();
APIconnection.setDoInput(true);
APIconnection.connect();
callbackParser = jsonFactory.createJsonParser(APIconnection.getInputStream());
response = objectMapper.readValue(callbackParser, User.class);
// Catch a JsonMappingException :
Can not deserialize instance of java.util.Calendar out of START_OBJECT token at [Source: org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream@4082c7a0; line: 1, column: 33] (through reference chain: classes.models.User["registredAt"])

問題は、コールバックregistredAtオブジェクトがjson_encoded PHP\DateTimeインスタンスであるということだと思います...registredAtJava Userオブジェクトのタイプを変更する必要がありますか?または、コンストラクターに何かが欠けていますか?

4

2 に答える 2

1

日付とカレンダーの標準 ISO-8601 文字列表現を使用する必要があります。または、JSON オブジェクトを使用する場合は、独自JsonDeserializerGregorianCalendar型を作成します。

于 2012-07-10T19:48:52.507 に答える
1

最後に、問題を解決するために次のようなものを作成しました。

public User(HashMap<String, Object> attributes) {
    this.id = (String) attributes.get("id");
    HashMap <String, Object> registredAt = (HashMap<String, Object>) attributes.get("registredAt");
    IMDateFormatter formatter = new IMDateFormatter();
    this.registredAt = formatter.getCalendarFromJson((String) registredAt.get("date"));
    [...]
}

IMDateFormatter を使用して、簡単な方法で日付をフォーマットします。

public class IMDateFormatter extends DateFormat {
    public IMDateFormatter() {
    }

    @Override
    public StringBuffer format(Date arg0, StringBuffer arg1, FieldPosition arg2) {

        return null;
    }

    @Override
    public Date parse(String dateFromJson, ParsePosition arg1) {

        return null;
    }

    public GregorianCalendar getCalendarFromJson(String dateFromJson) {
        GregorianCalendar calendarFromJson = null;

        try {
            GregorianCalendar tmpCalendar = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
            tmpCalendar.setTime(df.parse(dateFromJson));
            calendarFromJson = tmpCalendar;
        }
        catch (NullPointerException NPE) {
            NPE.printStackTrace();
        }
        catch (ParseException PE) {
            PE.printStackTrace();
        }

        return calendarFromJson;
    }
}
于 2012-07-11T15:40:08.810 に答える