VovaG
VovaG

Reputation: 51

Parse GitHub API response in Java

I have the following structure of JSON(github api response https://api.github.com/users/vGrynishyn/repos)

[
  {
    "id": 102121086,
    "name": "DolynaPlus",
    "full_name": "vGrynishyn_DolynaPlus",
    "owner": {
      "login": "vGrynishyn",
      "id": 26795358,
      ............
    },
    "private": false,
    "html_url": "https://github.com/vGrynishyn/DolynaPlus",
    "description": null,
    ..............
  },
  {
    "id": 103648881,
    "name": "GoogleSearchMaven",
    "full_name": "vGrynishyn/GoogleSearchMaven",
    "owner": {
      "login": "vGrynishyn",
      "id": 26795358,
      .........
    },
    "private": false,
    "html_url": "https://github.com/vGrynishyn/GoogleSearchMaven",
    "description": null,
    ...................
  }
]

I tried the following code to get array and received exception - cannot convert to array

JSONArray jsonArray = new JSONArray(json_response);

I tried parse it by Jackson but I have not needed result. I received results only for first section and exception appeared Parser exception

        JsonFactory fac = new JsonFactory();
        JsonParser jp = null;
        jp = fac.createParser(json_response);
        StringBuilder strPar = new StringBuilder(" ");
        jp.nextValue();
        while(jp.nextValue() != JsonToken.END_ARRAY){
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = jp.getCurrentName();
                jp.nextToken();

                if (fieldName.equals("id")){
                    strPar.append(jp.getText()+"; ");
                }else if (fieldName.equals("name")){
                    strPar.append(jp.getText()+"; ");
                }else if (fieldName.equals("full_name")){
                    strPar.append(jp.getText()+"; ");
                }
            }
        }

How I can parse it. Please help.

Upvotes: 1

Views: 1235

Answers (2)

neo73
neo73

Reputation: 464

The following code works fine:

public class JSONParse {

public static void main(String[] args) throws Exception {
    String url = "https://api.github.com/users/vGrynishyn/repos";
    String data = readUrl(url);
    JSONArray jsonArr = new JSONArray(data);

    for (int i = 0; i < jsonArr.length(); i++) {
        JSONObject jsonObj = jsonArr.getJSONObject(i);

        System.out.println(jsonObj.get("name"));
    }

}

private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1)
            buffer.append(chars, 0, read);

        return buffer.toString();
    } finally {
        if (reader != null)
            reader.close();
    }
}
}

OUTPUT:

DolynaPlus
GoogleSearchMaven
GoogleTesting
RadioTakt
Testlibrary

Upvotes: 1

Ruchira Randana
Ruchira Randana

Reputation: 4179

From the error message, it seems obvious that your JSON might be having an incorrect character. (The JSON you've posted is also not a valid JSON)

Please post the actual JSON and maybe I can help further :)

Upvotes: 0

Related Questions