JSON和Eclipse - 为什么抛出异常?

问题描述:

我知道一个关于java的相当数量,但是对于Slick2d和JSON是新手。我无法弄清楚为什么在尝试加载.json映射文件时抛出异常。我使用的是eclipse,slick2d,lwjgl和json。我内置的地图文件,它位于日食下的RES /地图/ 这里是堆栈跟踪JSON和Eclipse - 为什么抛出异常?

java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONArray 
at cardboard.world.World.load(World.java:33) 
at cardboard.Engine.initStatesList(Engine.java:49) 
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164) 
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393) 
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317) 
at cardboard.Engine.main(Engine.java:31) 

这里是执行代码

try { 
     World.load("res/maps/world.json"); 
    } catch (Exception e) { 
     System.err.println("Map does not exist"); 
     System.exit(0); 
    } 

这里是世界级的

import java.io.FileReader; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.newdawn.slick.Image; 
import org.newdawn.slick.SpriteSheet; 

import cardboard.Resources; 

public class World { 

public static Image[][] solids; 
public static int WIDTH; 
public static int HEIGHT; 

public static void load(String path) throws Exception { 

    JSONParser parser = new JSONParser(); 
    Object obj = parser.parse(new FileReader(path)); 
    JSONObject jObj = (JSONObject) obj; 

    JSONArray layers = (JSONArray) jObj.get("layers"); 
    int amount = layers.size(); 

    for (int i = 0; i < amount; i++) { 
     JSONObject layer = (JSONObject) layers.get(i); 
     String type = (String) layer.get("name"); 

     if (type.equals("solids")) { 
      solids = parse((JSONArray)layer.get("data")); 
     } else if (type.equals("spawns")) { 
      // Spawn point code 
     } 
    } 

} 

private static Image[][] parse(JSONArray arr) { 

    Image[][] layer = new Image [WIDTH][HEIGHT]; 
    int index; 

    for (int y = 0; y < WIDTH; y++) { 
     for (int x = 0; x < HEIGHT; x++) { 
      index = (int)((long)arr.get((y * WIDTH) + x)); 
      layer[x][y] = getSpriteImage(index); 
     } 
    } 

    return layer; 
} 

private static Image getSpriteImage(int index) { 
    if (index == 0) return null; 
    index -= 1; 

    SpriteSheet sheet = Resources.getSprite("tileset"); 
    //int vertical = sheet.getVerticalCount(); 
    int horizontal = sheet.getHorizontalCount(); 

    int y = (index/horizontal); //vert? 
    int x = (index % horizontal); 

    return sheet.getSubImage(x, y); 
} 
} 

Eclipse没有发生错误我刚刚收到我写的“Map does not exist”的错误信息如果你可以看看,这将是超级!由于

情况下你需要Resources.java

package cardboard; 

import java.util.HashMap; 
import java.util.Map; 

import org.newdawn.slick.Image; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.Sound; 
import org.newdawn.slick.SpriteSheet; 

import cardboard.world.Tile; 

public class Resources { 

private static Map<String, Image> images; 
private static Map<String, SpriteSheet> sprites; 
private static Map<String, Sound> sounds; 

public Resources() { 

    images = new HashMap<String, Image>(); 
    sprites = new HashMap<String, SpriteSheet>(); 
    sounds = new HashMap<String, Sound>(); 

    try { 
     sprites.put("tileset", loadSprite("res/tileset.png", Tile.SMALL_SIZE, Tile.SMALL_SIZE)); 
    } catch (SlickException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public static Image loadImage(String path) throws SlickException { 
    return new Image(path, false, Image.FILTER_NEAREST); 
} 

public static SpriteSheet loadSprite(String path, int tw, int th) throws SlickException { 
    return new SpriteSheet(loadImage(path), tw, th); 
} 

public static SpriteSheet getSprite(String getter) { 
    return sprites.get(getter); 
} 

public static Image getSpriteImage(String getter, int x, int y) { 
    return sprites.get(getter).getSubImage(x,y); 
} 

public static Image image(String getter) { 
    return sprites.get(getter); 
} 

public static Image getImage(String getter) { 
    return images.get(getter); 
} 

public static Sound getSound(String getter) { 
    return sounds.get(getter); 
} 
} 
+1

你会得到什么例外?用'System.out.println'打印例如 – Enigo

+1

只是打印出异常 –

+1

请在你的问题中加入打印堆栈。 @ squirt706谢谢 –

有两种例外的java介绍。

A)经过异常(编译时间): - 处理在编译时例外只以检测任何外部组件或资源相关性(如FileNotFoundIOException)。

B)未经检查的异常(运行时异常): - 此异常来到同时,因为任何意外情况的任何逻辑错误或任何错误(IndexOutOfBoundNullPointer等)

现在您的问题

为什么在尝试加载.json映射文件时抛出异常?

如果您尝试加载在你的程序的任何文件.json这是从任何存储区(硬盘,云等)加载的外部资源。因此,存储区域可能不存在File的机会。所以,为了处理这种不确定的情况,你的程序应该准备好处理这种情况(这不是一个逻辑错误,它是对你的代码的间接部分的其他资源的依赖)。

这就是把这种东西放在Checked Exception段的原因。

当你发现异常e时,你应该用e做一些事情。要获得有关抛出异常的更多信息,请使用以下代码:

} catch (Exception e) { 
    e.printStackTrace(); 
    System.exit(1); // Because 1 means failure, 0 means OK. 
} 
+0

谢谢,所以我更新了我的帖子。我查找了如何读取堆栈跟踪(从下到上),但无法进行任何进展World:33 is solids = parse((JSONArray)layer.get(“data”)); – squirt706

+0

而且例外情况非常明显。 JSON解析器将'data'字段解释为一个字符串(因为JSON数据看起来像它),但是您强制Java将它解释为JSONArray。查看JSON数据以及它的结构。 –