String for Array/List

Asked

Viewed 182 times

1

I got the following String:

[{"Monstro":"Lobo","HP":100,"Level":2},{"Level":"1","HP":"100","Monstro":"Bruxa"}]

And I need to put her inside a Array ou List to take the values of each monster.

  • How do I do it?

I’m sorry if the doubt seems obvious.

OBS: are data from JSON that are within the […] as normal as this and has the },{ between them that seprar the 2 different values that are within the [...].

  • The best way to do this is by creating a class and parsing in json for a class object. Make sure you want to do so?

  • yes I use the org.json api which does not support parce and I am running some tests before I even assemble that part of the api. taking advantage as a challenge to myself.

  • Need to be an array? Cannot use List?

  • @Renan can be any way for me to choose which of the two dice to pick.

  • Okay, is your JSON wrong anyway or was it time to move here to the question? There are different keys in the objects Monstro and Monstro:.

  • @Renan I didn’t even realize I had to tidy up td since Ctrl+c/Ctrl+v bugged my keyboard

Show 1 more comment

1 answer

1


I would follow the same idea of comment from @Francisco, create an object that maps the monster’s attributes: nome, hitpoints and level, although I would not use them per hour in my application. But, if the goal is only to get all the key values Monstro, can do so:

public final List<String> getMonstros(String json){
    List<String> monsters = new ArrayList<>();
    try {
        new JSONArray(json).forEach(item -> {

            JSONObject object = (JSONObject) item;
            if(object.has("Monstro"))
                monsters.add(object.getString("Monstro"));

        });  
    } catch(Exception ex){
        // Tratamento de exceção.
    }
    return monsters;
}
final String json = "[{\"Monstro\":\"Lobo\",\"HP\":100,\"Level\":2}, {\"Level\":\"1\",\"HP\":\"100\",\"Monstro\":\"Bruxu\"}]";
System.out.println(getMonstros(json)); // [Lobo, Bruxu]
  • your answer gave me an idea if you use the same code by exchanging if(object.has("Monstro"))&#xA; monsters.add(object.getString("Monstro")); for f(object.has("hp"))&#xA; monsters.add(object.getString("hp")); it returns in order? if it returns in order I already have an idea of how to do the parcer of org.json

  • It wouldn’t even work because the attribute HP is capitalized and is a whole type, would have to use object.getInt("HP"). Even so, I would not return in order (nor this of my answer returns ordered), I would have to use the method Collections#sort() for this. And of course, instead of a list of strings, it would be a list of integers.

  • then does not return in the order that is in the json string?

  • to receiving error in forEach

  • No, returned in order because the string is with the attributes Lobo and Bruxu in sequence. Which error is giving in forEach? Which version of Java is running?

  • java: jdk1.8.0_151, jre1.8.0_151 forEach erro:cannot find symbol and at the time to give println give the error: non-static method getMonstros(String) cannot be referenced from a static context

  • I will have to turn off the pc now tomorrow see if I fix, if you have Discord add#1011

  • You’re probably calling the method getMonstros() of a static method. Simply switch to public static final Lista<String> getMonstros().

  • now only give the foreach error

  • What mistake.... ?

  • dx quiet I arranged another way to do this vlw by help

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.