-4

Hey guy's so am trying to parse this huge text in getting the stuff i want like the descreption id,icon etc, But i can't seem to be able to get the rest am trying to get the icon now but keeps telling me :

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)

Am trying to get the (iwd1D) only with the "" stuff any help is great thanks!

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);
  }
}
Punxor
  • 27
  • 7
  • 5
    Please don't parse JSON yourself. Use a JSON parser. http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Ruan Mendes Dec 17 '14 at 04:48

1 Answers1

0
skill.substring(startICO, startICO - endIcon);

In this value of startICO would be greater than startICO - endIcon

Hence the StringIndexOutofBoundsException

abhati
  • 309
  • 1
  • 6
  • So you're saying it should be endIcon - startIcon ? Please add a disclaimer telling the OP to use a JSON parser – Ruan Mendes Dec 17 '14 at 04:56