2

我正在开发一个小型 Web 应用程序。

那是我的问题:

当用户尝试在我的数据表中添加新记录时执行此操作

@RequestMapping(value="/saveJokerRule", method= RequestMethod.POST, consumes= "application/json", produces = "application/json")
@ResponseBody
public String saveJokerRule(@RequestBody String json) {
    System.out.println("JSON EDITOR:" + json.toString());
    EditorResponse editorResponse = new EditorResponse();

    JokerRuleForm jokerRuleForm = new GsonBuilder().serializeNulls().create().fromJson(json, JokerRuleForm.class);

    ...
}

这是服务器接收并由系统调用打印的有效 json:

{"action":"create","data":{"0":{"jokerRule":{"group":1,"campaignPhase":"","dailyLimit":"","weeklyLimit":"","monthlyLimit":"","validFrom":"","activity":1}}}}

这就是 JokerRuleForm 类

public class JokerRuleForm {
    @Expose
    String action;

    @Expose
    @SerializedName("data")
    Map<String, JokerRuleView> data;

    ...
}

这就是 JokerRuleView 类

public class JokerRuleView {

String idJokerRule;

private AgentGroup group;

private JokerRule jokerRule;

private Activity activity;

public class JokerRule{

    private String campaignPhase;
    private Integer dailyLimit;
    private Integer weeklyLimit;
    private Integer monthlyLimit;
    private Date validFrom;
    private Date dateUpdate;
    private String group;
    private String activity;
    /**
     * @return the campaignPhase
     */
    public String getCampaignPhase() {
        return campaignPhase;
    }
    /**
     * @param campaignPhase the campaignPhase to set
     */
    public void setCampaignPhase(String campaignPhase) {
        this.campaignPhase = campaignPhase;
    }
    /**
     * @return the dailyLimit
     */
    public Integer getDailyLimit() {
        return dailyLimit;
    }
    /**
     * @param dailyLimit the dailyLimit to set
     */
    public void setDailyLimit(Integer dailyLimit) {
        this.dailyLimit = dailyLimit;
    }
    /**
     * @return the weeklyLimit
     */
    public Integer getWeeklyLimit() {
        return weeklyLimit;
    }
    /**
     * @param weeklyLimit the weeklyLimit to set
     */
    public void setWeeklyLimit(Integer weeklyLimit) {
        this.weeklyLimit = weeklyLimit;
    }
    /**
     * @return the monthlyLimit
     */
    public Integer getMonthlyLimit() {
        return monthlyLimit;
    }
    /**
     * @param monthlyLimit the monthlyLimit to set
     */
    public void setMonthlyLimit(Integer monthlyLimit) {
        this.monthlyLimit = monthlyLimit;
    }
    /**
     * @return the validFrom
     */
    public Date getValidFrom() {
        return validFrom;
    }
    /**
     * @param validFrom the validFrom to set
     */
    public void setValidFrom(Date validFrom) {
        this.validFrom = validFrom;
    }
    /**
     * @return the dateUpdate
     */
    public Date getDateUpdate() {
        return dateUpdate;
    }
    /**
     * @param dateUpdate the dateUpdate to set
     */
    public void setDateUpdate(Date dateUpdate) {
        this.dateUpdate = dateUpdate;
    }
    /**
     * @return the group
     */
    public String getGroup() {
        return group;
    }
    /**
     * @param group the group to set
     */
    public void setGroup(String group) {
        this.group = group;
    }
    /**
     * @return the activity
     */
    public String getActivity() {
        return activity;
    }
    /**
     * @param activity the activity to set
     */
    public void setActivity(String activity) {
        this.activity = activity;
    }

}

public class Activity {
    String idActivity;
    String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the idGroup
     */
    public String getIdActivity() {
        return idActivity;
    }

    /**
     * @param idGroup the idGroup to set
     */
    public void setIdActivity(String idActivity) {
        this.idActivity = idActivity;
    }
}

public class AgentGroup {

    String idGroup;
    String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the idGroup
     */
    public String getIdGroup() {
        return idGroup;
    }

    /**
     * @param idGroup the idGroup to set
     */
    public void setIdGroup(String idGroup) {
        this.idGroup = idGroup;
    }

}

/**
 * @return the idJokerRule
 */
public String getIdJokerRule() {
    return idJokerRule;
}

/**
 * @param idJokerRule the idJokerRule to set
 */
public void setIdJokerRule(String idJokerRule) {
    this.idJokerRule = idJokerRule;
}

/**
 * @return the agentGroup
 */
public AgentGroup getGroup() {
    return group;
}

/**
 * @param agentGroup the agentGroup to set
 */
public void setGroup(AgentGroup group) {
    this.group = group;
}

/**
 * @return the jokerRule
 */
public JokerRule getJokerRule() {
    return jokerRule;
}

/**
 * @param jokerRule the jokerRule to set
 */
public void setJokerRule(JokerRule jokerRule) {
    this.jokerRule = jokerRule;
}

/**
 * @return the activity
 */
public Activity getActivity() {
    return activity;
}

/**
 * @param activity the activity to set
 */
public void setActivity(Activity activity) {
    this.activity = activity;
}

}

 JokerRuleForm jokerRuleForm = new GsonBuilder().serializeNulls().create().fromJson(json, JokerRuleForm.class);

执行此行时,我得到一个 NumberFormatException 空字符串,我认为它是 dailyLimit 或 date 的错误,因为它是空的,并且 fromJson() 方法不能做他需要做的事情。

我读过一些关于 TypeAdapter 似乎适合我的情况的内容,但我真的不知道如何继续,我不确定这是一个有效的解决方案。

有人能帮我吗?

4

1 回答 1

1

例如,限制字段dailyLimit是 JSON 中的空字符串。当遇到 int 或 long #472 问题的空值时,在 JsonParseException 中建议了这一点,但 Gson 团队将其关闭,因为将其解析""int.

其中一位用户在他的评论中提供了一个解决方案,可以轻松解析数值。我不会自己走这条路并修复 JSON,但这取决于你:

public static final TypeAdapter<Number> UNRELIABLE_INTEGER = new TypeAdapter<Number>() {
    @Override
    public Number read(JsonReader in) throws IOException {
        JsonToken jsonToken = in.peek();
        switch (jsonToken) {
            case NUMBER:
            case STRING:
                String s = in.nextString();
                try {
                    return Integer.parseInt(s);
                } catch (NumberFormatException ignored) {
                }
                try {
                    return (int)Double.parseDouble(s);
                } catch (NumberFormatException ignored) {
                }
                return null;
            case NULL:
                in.nextNull();
                return null;
            case BOOLEAN:
                in.nextBoolean();
                return null;
            default:
                throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
        }
    }
    @Override
    public void write(JsonWriter out, Number value) throws IOException {
        out.value(value);
    }
};
public static final TypeAdapterFactory UNRELIABLE_INTEGER_FACTORY = TypeAdapters.newFactory(int.class, Integer.class, UNRELIABLE_INTEGER);

Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(UNRELIABLE_INTEGER_FACTORY)
    .create();
于 2019-01-23T10:40:26.237 回答