Selecting data in Java Curl

Asked

Viewed 606 times

1

I would like to know how to select data using gson library.

    BufferedReader reader = null;
    URL endereco = new URL("https://prod.api.pvp.net/api/lol/"+Server+"/v1.4/summoner/by-name/"+Jogador+"?api_key="+API_KEY);
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(endereco.openStream()));
    Gson obj = new Gson();

    System.out.println(br.readLine());
    } finally {
        if (reader != null) {
            try {
                reader.close(); 
            } catch (IOException ignore) {}
        }
    }

I had as a result:

{ "exemplo" : { 
      "id" : 1473077,
      "name" : "Exemplo",
      "profileIconId" : 25,
      "revisionDate" : 1355854041000,
      "summonerLevel" : 13
   } 
}
  • What did Oce want as a result? Explain more about your problem.

  • I’d like to take the data and manipulate it, with this get I get the whole result, I’d like to separate. Select id and store, select name and etc.

1 answer

1


As I don’t have an API key to test, I checked the past site and saw that everything is in one single line. So you wouldn’t even need GSON for parse (don’t use libraries if you don’t need them)

package data;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class semGsonExemplo {

    public static void main(String[] args){
        try {
        URL endereco = new URL("http://kyllo.com.br/GSON.exemplo");
        BufferedReader br = new BufferedReader(new InputStreamReader(endereco.openStream()));
        String data = br.readLine();
        System.out.println("id = " + parseSemGson(data,"id"));
        System.out.println("name = " + parseSemGson(data,"name"));
        System.out.println("profileIconId = " + parseSemGson(data,"profileIconId"));
        System.out.println("revisionDate = " + parseSemGson(data,"revisionDate"));
        System.out.println("summonerLevel = " + parseSemGson(data,"summonerLevel"));
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    public static String parseSemGson(String linha,String campo){
        linha = linha.replaceAll("\"","");
        int indice = linha.indexOf(campo) + 3 + campo.length();
        int ultimo_char = linha.indexOf(",", indice)!=-1?linha.indexOf(",", indice):linha.indexOf("}", indice);
        return linha.substring(indice,ultimo_char);
    }
}

Note that I put your example on my site, just so it’s in the same format as the API gives you.

Browser other questions tagged

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