Selecting data in cURL Java

1

I would like to know how to select the data using the 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 got this as a result:

{ "exemplo" : { 
      "id" : 1473077,
      "name" : "Exemplo",
      "profileIconId" : 25,
      "revisionDate" : 1355854041000,
      "summonerLevel" : 13
   } 
}
    
asked by anonymous 20.06.2014 / 20:17

1 answer

1

As I do not have an API key to test, I checked the site last and saw that everything is in a single line . So I would not even need GSON for parse (do not use libraries if they are not needed)

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 that it is in the same format as the API passes you.

    
21.06.2014 / 01:09