0

Just to start off, I'm new to Java. I'm trying to make a notification system for Twitch using their API and an Arduino, and I'm currently stuck at the start pretty much. I am able to access the API and get information, but I don't know how to convert it into something useful. This is my code currently.

public class commandRun {

public static void main(String[] args) {
    Process p;
    try {
        p = Runtime.getRuntime().exec("cmd /c curl -H \"Client-ID: clientID\" -X GET \"https://api.twitch.tv/helix/users/follows?first=1&to_id=channelID\n");

        p.waitFor();
        BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
        String twitchFollower;

        while((twitchFollower = reader.readLine()) != null) {
            System.out.println(twitchFollower);
        }
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

This returns

{
  "total": 106,
  "data": [
    {
      "from_id": "followerID",
      "from_name": "follower",
      "to_id": "channelid",
      "to_name": "channel",
      "followed_at": "2019-10-06T21:03:03Z"
    }
  ],
  "pagination": {
    "cursor": "dontknowifthisissensitiveinfo"
  }
}

I want to be able to take specific info out of string such as the followers' name.

jrook
  • 3,459
  • 1
  • 16
  • 33
  • Please consider using a JSON library. Here is a list: https://javarevisited.blogspot.com/2016/09/top-5-json-library-in-java-JEE.html – jrook Oct 07 '19 at 23:32
  • Not related to your question, but is there are reason why you are using `cURL` when the JDK provides other options, such as [URLConnection](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URLConnection.html) ? – Abra Oct 08 '19 at 01:47
  • @Abra whenI read through the Twitch API guide briefly they used cURL in all their examples so I just figured I'd find a way to use the command line in Java. As I said, I'm new to Java so I didn't even know of the way you mentioned. I'm going to read into it and I might switch over. – NikBrennan Oct 08 '19 at 11:21

1 Answers1

0

There are many JSON libraries can achieve this as jrook's comment.
And following sample code shows how the most 2 popular libraries - Jackson and Gson - get the value of specific field. (I assumed that the key of follower's name is from_name.)

// Jackson
ObjectMapper mapper = new ObjectMapper();
String followerName = mapper.readTree(jsonStr)
        .get("data")
        .get(0)
        .get("from_name")
        .asText();
System.out.println(followerName);

// Gson     
followerName = new JsonParser().parse(jsonStr)
        .getAsJsonObject()
        .get("data")
        .getAsJsonArray()
        .get(0)
        .getAsJsonObject()
        .get("from_name")
        .getAsString();
System.out.println(followerName);

Console output:

follower
follower

LHCHIN
  • 3,679
  • 2
  • 16
  • 34