0

I'm having a bit of trouble parsing formatting my json correctly and I was wondering if someone could show how to parse the data correctly in this instance:

SOURCE:

  try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            InputStream is = conn.getInputStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            for(String line = r.readLine(); line != null; line = r.readLine()){
                sb.append(line);
            }
            JSONArray jsonArray = new JSONArray(sb.toString());
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject obj = jsonArray.getJSONObject(i);
                String sha = obj.getString("sha");
                String name = obj.getString("name");
                String message = obj.getString("message");
                Log.d("sha", sha);
                Log.d("name", name);
                Log.d("message", message);

            }
        } 

ERROR:

"No value for name"

JSON DATA:

http://pastebin.com/0FJUGm3L

Guy Richards
  • 27
  • 3
  • 14

2 Answers2

1

If you load your JSON into this JSON Viewer tool, you will recognize that you have the following JSON structure:

enter image description here

Now, since you want to get sha, commit message, and commiter name, you will need to go deep into json's object structure.

  1. First, you will need to loop the entire json array:

    for(int i = 0; i < jsonArray.length(); i++){
        JSONObject currentObj = jsonArray.getJSONObject(i);
    
  2. Inside the loop, you need to find where the "sha" is. Take a look at the structure again and you'll notice it is right under the object. So you just need to use getString():

    String sha = currentObj.getString("sha");
    
  3. To get commit message, since it is under commit object, you will need to read that object first, before reading message:

    JSONObject objCommit = currentObj.getJSONObject("commit");
    String commitMessage = objCommit.getString("message");
    
  4. The final piece is to get author's name. Again, notice that name is stored under author object. So, you will need to read that first. And because author object is stored under commit object, you get author by reading commit object via objCommit:

    JSONObject objAuthor = objCommit.getJSONObject("author");
    String authorName = objAuthor.getString("name");
    
  5. The rest is up to you :)

Selfless plug: Also, if you are having a hard time reading or traversing JSON, the SO question can come in handy: JSON Parsing Error

Community
  • 1
  • 1
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

All keys name,message,.. is in author JSONObject. gett all values from author object as:

 for(int i = 0; i < jsonArray.length(); i++){
                JSONObject obj = jsonArray.getJSONObject(i);
                String sha = obj.getString("sha");
                // get commit  object
                JSONObject objcommit = obj.getJSONObject("commit"); 
                // get author  object
                JSONObject objauthor = objcommit.getJSONObject("author");
                String name = objauthor.getString("name");
                String message = objauthor.getString("message");
                Log.d("sha", sha);
                Log.d("name", name);
                Log.d("message", message);

            }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213