How to separate a string into several in java?

Asked

Viewed 467 times

2

My problem is basically the following, I have a JSON code that takes the data from a URL, and returns it to me (The values are in Long)

528593
444218
5693595
2466912
2466435

However, it returns this to me in a single variable (in this case, the "wtfbo"), and I need to divide this into a string for each one to fit into the interface I have already assembled. In case, I am using this code at the moment

  [...]
  Iterator<?> it = eat.iterator();
  while (it.hasNext()) {
      JSONObject dash = (JSONObject) it.next();
      Long wtfb10 = (Long) dash.get("teamId");

  if(wtfb10==100){
           Long wtfbo = (Long) dash.get("summonerId");
            String play = (String) dash.get("summonerName");
            String ko = wtfbo.toString();
            System.out.println(ko);
 }}
 [...]

Any idea?

//EDIT: In short, the "Long wtfbo" will return 5 different summonerId, only everything in it, I need it to return in 5 different strings, each one having a summonerID.

  • How is the structure of JSON?

  • http://pastebin.com/TjjJ4jWk is identical to this, in which case I need to take the summonersID/summonerName of the 10 players.

  • In the JSON you sent each ID there is a different object. What you mean by "returns it in a single variable"?

  • is that all 10 different summonerId, return everything on "wtfbo"

  • Related: http://answall.com/questions/49838/howto get m%C3%Baltiplos-jsonobject-com-a-mesma-key

1 answer

2

I can not comment then... rs.
I did not understand for sure if you only the codes of the "summonerId" or the objects of the participants that contain this attribute.

To pick up an array of participants' objects you could do something like the code below. Considering the json that is in Pastebin, to catch the participants:


JSONArray participantes = jsonData.getJSONArray("participants");
//manipula valores dos participantes
for (int i=0; i < participantes.length(); i++) {
    //participante no índice i
    JSONObject player = arrFilmes.getJSONObject(i);
    Long wtfbo = (Long) player.get("summonerId");
    ...
}
  So I would take the data from each participant and I could structure it any way I wanted. I hope it helps in something and good luck.

  • Until that part I get, the problem is that it returns all the summonerId values in "wtfbo", and I would need it to return each one in a different string.

  • Why not create a String List and add it? type List<String> ids = new Arraylist<String>(); .

  • Doing so: while (it.hasNext()) { Jsonobject Dash = (Jsonobject) it.next(); Long wtfb10 = (Long) Dash.get("teamId"); if(wtfb10=100){ Long wtfbo = (Long) Dash.get("summonerId"); Arraylist<Long> ids = new Arraylist<Long>(); ids.add(wtfbo); } and he stored everything at the 0 value of the list.

  • Create the Arraylist<Long> ids = new Arraylist<Long>(); outside the while loop, as it is being recreated with each interaction, so the values 0 probably..

  • He keeps saving all the values together at the same value of the list.

  • Try printing the value before adding to the list to see if it’s not all coming at once or something like that, because in theory it’s supposed to work.

Show 1 more comment

Browser other questions tagged

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