杰克逊数据绑定MismatchedInputException

时间:2019-04-07 15:46:44

标签: java

使用spring配置将数据从json读取到spring bean中,它适用于简单类型,但是当尝试读取数组时出现上述错误 这是我的配置

class Player(pygame.sprite.Sprite):

    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.health = 10
        self.visible = True
        self.explosions = False
        self.explosionc = 0
        self.hitbox = (self.x + 5,self.y + 10,60,60)
        self.canshoot = True
        self.dead = False

        self.frame_delay = 200 # 200 milliseconds


    def draw(self,win):
        global bgvel

        if self.y > 600:
            win.blit(EFTNA,(self.x,self.y))
            for enemy in enemies:
                enemy.vel = 2
                #bgvel = 1
        elif self.y <= 600 and self.y > 400:
            win.blit(EFTAL,(self.x,self.y))
            for enemy in enemies:
                enemy.vel = 3
                #bgvel = 2
        elif self.y <= 400 and self.y > 350:
            win.blit(EFTAM,(self.x,self.y))
            for enemy in enemies:
                enemy.vel = 4
                #bgvel = 3
        elif self.y <= 450:
            win.blit(EFTAH,(self.x,self.y))
            for enemy in enemies:
                enemy.vel = 5
                #bgvel = 4
        self.hitbox = (self.x + 5,self.y + 10,60,60)
        #pygame.draw.rect(win,(255,0,0),self.hitbox,2)

        if self.y + self.height > HEIGHT:
            self.y -= self.vel
        if self.x < 0:
            self.x += self.vel
        if self.x + self.width > WIDTH:
            self.x -= self.vel

        if self.explosions == True:

            current_time = pygame.time.get_ticks()
            if current_time - self.start_delay >= self.frame_delay:
                self.start_delay = current_time

                print("I exploded")
                self.canshoot = False
                self.dead = True

                self.explosionc = (self.explosionc + 1) % 6

                if self.vel > 0: # ???
                    win.blit(Expl[self.explosionc],(self.x,self.y))

            self.visible = False

    def hit(self):
        if self.health > 0:
           self.health -= 1
        else:
           self.explosions = True
           self.start_delay = 0 # or pygame.time.get_ticks()

我决定按照建议放入一个容器,这样可以让我的配置看起来像这样吗?因为我将使用这些数据来创建一些弹簧控制器。 POJO课

@Bean
Container container() throws IOException {

    ObjectMapper objectMapper = new ObjectMapper();

    return objectMapper.readValue(getClass().getClassLoader().getResource("book.json"), Container.class);
}

这是json数据

public class Container {
String requestedUrl;
  List<Item> items;}

如何编辑配置以使其正常工作

1 个答案:

答案 0 :(得分:2)

您正在传递以{}为界的json对象,但尝试将其转换为数组。有了这个json,您应该转换为另一个类,例如

public class Container {
    private String requestedUrl;
    private List<Item> items;

    // getters/setters
}