试图在JAVA中解析一个字符串

时间:2014-12-17 04:45:41

标签: java

嘿家伙所以我试图解析这个巨大的文字,以获得我想要的东西,如descreption id,icon等,但我似乎无法得到剩下的我想要得到现在的图标,但一直告诉我:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -18
at java.lang.String.substring(String.java:1954)
at com.company.Main.main(Main.java:19)

我试图仅使用""来获取(iwd1D)任何帮助都非常感谢!

public static void main(String[] args) {
    String skill = "[{\"id\":\"16\",\"icon\":\"iwd1D\"ref\":\"aa\",\"mp\":\"0\",\"nam\":\"Auto attack\",\"anim\":\"Attack1,Attack2\",\"desc\":\"A basic attack, taught to all adventurers.\",\"isOK\":true,\"range\":\"301\",\"fx\":\"m\",\"damage\":1,\"auto\":true,\"tgt\":\"h\",\"typ\":\"aa\",\"dsrc\":\"AP2\",\"cd\":\"2000\"}";

    //Getting id which is 16 works fine
    int startID;
    startID = skill.indexOf("\"id\"") + 6;
    int endID = skill.indexOf(":\"", startID);
    String id = skill.substring(startID, endID - startID);
    System.out.println(id);

    //Getting icon which is iwd1D can't seem to get it work
    int startICO;
    startICO = skill.indexOf(",\"icon\"") + 7;
    int endIcon = skill.indexOf(":\"", startICO);
    String icon = skill.substring(startICO, startICO - endIcon);
    System.out.println(icon);
  }
}

1 个答案:

答案 0 :(得分:0)

skill.substring(startICO, startICO - endIcon);

startICO的值大于startICO - endIcon

因此StringIndexOutofBoundsException