JsonMappingException:意外的令牌(START_OBJECT)

时间:2015-07-06 09:57:17

标签: java json jackson

我与Codehaus Jackson的用户有问题。我有一个具有下一个属性和mapper声明的对象:

if(angular.isNumber($scope.Nuser.centerCode)){
   alert('Center Code is a number');
}else {
   alert('Center Code is not a number');
}

从另一个类使用:

public class AuthenticatedPrincipal implements Serializable, Principal {
      @JsonIgnore
      private final static ObjectMapper mapper = new ObjectMapper().enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY).enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)
      .setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL).setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);

  private String name;

  private Collection<String> roles;

  private Collection<String> groups;

  private boolean adminPrincipal;
...
  @JsonIgnore
  public String serialize() {
    try {
      return mapper.writeValueAsString(this);
    } catch (IOException e) {
      throw new RuntimeException("Unable to serialize Principal:" + toString(), e);
    }
  }

  @JsonIgnore
  public static AuthenticatedPrincipal deserialize(String json) {
    try {
      return mapper.readValue(json, AuthenticatedPrincipal.class);
    } catch (IOException e) {
      throw new RuntimeException("Unable to serialize Principal:" + json, e);
    }
  }
}

当我执行生成如下字符串的功能时:

public class AuthRequest {

  @Transient
  private AuthenticatedPrincipal principal;
  @PreUpdate
  @PrePersist
  public void encodePrincipal() {
    if (principal != null) {
        this.encodedPrincipal = principal.serialize();
    }
  }

  @PostLoad
  @PostPersist
  @PostUpdate
  public void decodePrincipal() {
    if (StringUtils.isNotBlank(encodedPrincipal)) {
        this.principal = AuthenticatedPrincipal.deserialize(encodedPrincipal);
    }
  }
}

当方法AuthenticatedPrincipal.deserialize(encodedPrincipal);被称为解析Json参数,但该方法失败并出现此错误:

Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

无效的JSON。它需要看起来像这样

{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

(开始时没有Principal: