Football data API

edited January 2017 in Library Questions

I am trying to get data from the football-data.org API. My code looks like this:

import java.net.*;

String Link = "http://" + "api.football-data.org/v1/teams/66/";

void setup()
{
  String[] results = null;
  try
  {
    URL url= new URL(Link);
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("X-Auth-Token","XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    connection.setRequestProperty("X-Response-Control","minified");

    results = loadStrings(connection.getInputStream());
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }

  if (results != null)
  {
    for (int i=0; i<results.length; i++) { System.out.format(results[i]); }
  }
}

The data is being printed like this:

{"id":66,"name":"Manchester United FC","shortName":"ManU","squadMarketValue":"534,250,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg"}

but I want it like this:

{"id":66,

"name":"Manchester United FC",

"shortName":"ManU",

"squadMarketValue":"534,250,000€",

"crestUrl":"http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg"}

Can someone please help me?

(I'm sorry, i'm new to processing)

Answers

  • Try changing line 24 to include a new line charater like this

    for (int i=0; i<results.length; i++) { System.out.format(results[i] + "\n"); }

  • Thanks for your answer, but the output stays the same. Is there an other way to change the layout?

  • edited January 2017

    @Narcekki --

    There is no way to test this without either a dummy response URL or an auth token, however it looks to me like each {"id"...} block is a single line in your for loop -- so adding line breaks between entries won't help.

    Is your end goal really to pretty-print this data to your sketch console? I'm guessing not. The API is returning JSON, so you should probably load it into a JSONObject and then save it to a file, or extract data from it, or whatever it is you want to do.

    If your goal really is to display it with linebreaks, perhaps you could try:

    1. parsing the response string into a JSONObject data object -- https://processing.org/reference/parseJSONObject_.html
    2. saving the JSONObject to a temporary .json file -- this will automatically be in "pretty" format with linebreaks -- https://processing.org/reference/saveJSONObject_.html
    3. load that .json file as a string -- https://processing.org/reference/loadStrings_.html
    4. print the string to the console with print
  • or just parse the data into an object and print the name:value pairs yourself, with the relevant decoration.

Sign In or Register to comment.