Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON array Read first element?

Tags:

java

json

arrays

I got an JSON Array for my Twitch Following user checker. I would like to get only the first date (cause thats the newest one) out of the array but everytime my code gets executed it just swap to the next "date" then.

How can i change this?

Code;

import org.jibble.pircbot.*;
import org.json.JSONException;
import org.json.simple.*;
import org.json.simple.parser.*;
import org.w3c.dom.ranges.RangeException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.*;



////////////////////////////////////////////////////////////////////////////////////
                // Twitch Follower Ticker
                ////////////////////////////////////////////////////////////////////////////////////

                private String readAll4(Reader rd) throws IOException {
                    StringBuilder sb = new StringBuilder();
                    int cp;
                    while ((cp = rd.read()) != -1) {
                      sb.append((char) cp);
                    }
                    return sb.toString();
                  }

                  public JSONObject readJsonFromUrl4(String url) throws IOException, JSONException {
                    InputStream is = new URL(url).openStream();
                    try {
                      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                      String jsonText = readAll4(rd);
                      JSONObject json = new JSONObject(jsonText);
                      return json;
                    } finally {
                      is.close();
                    }
                  }

                  public void FollowerTicker() throws IOException, JSONException {
                    json = readJsonFromUrl2("https://api.twitch.tv/kraken/channels/"+ownerchannel+"/follows");

                    JSONArray followerarray = json.getJSONArray("follows");

                    for(int n = 0; n < followerarray.length(); n++)
                    {
                        JSONObject followertime = followerarray.getJSONObject(n);
                        String ftime = followertime.getString("created_at");


                        int maxfollows = json.getInt("_total");


                    System.out.println("Total Follows : "+maxfollows);
                    System.out.println("Loop Follow Date: "+ftime);

                    }
              }

edited Code;

                private String readAll4(Reader rd) throws IOException {
                    StringBuilder sb = new StringBuilder();
                    int cp;
                    while ((cp = rd.read()) != -1) {
                      sb.append((char) cp);
                    }
                    return sb.toString();
                  }

                  public JSONObject readJsonFromUrl4(String url) throws IOException, JSONException {
                    InputStream is = new URL(url).openStream();
                    try {
                      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                      String jsonText = readAll4(rd);
                      JSONObject json = new JSONObject(jsonText);
                      return json;
                    } finally {
                      is.close();
                    }
                  }

                  public void FollowerTicker() throws IOException, JSONException {
                    json = readJsonFromUrl2("https://api.twitch.tv/kraken/channels/"+ownerchannel+"/follows");

                    JSONArray followerarray = json.getJSONArray("follows");

//                  for(int n = 0; n < followerarray.length(); n++)
                    {
                        JSONObject followertime = followerarray.getJSONObject(0);
                        String ftime = followertime.getString("created_at");
                        String fname = followertime.getJSONObject("user").getString("display_name");  
                        int maxfollows = json.getInt("_total");


                    System.out.println("Total Follows zurzeit: "+maxfollows);
                    System.out.println("Neustes Follower - Datum: "+ftime);
                    System.out.println("Neuster Follower Name: "+fname);

                    }
              }
like image 892
user3220962 Avatar asked Feb 23 '14 15:02

user3220962


People also ask

How do you access the first element of a JSON object array?

The Best Answer isvar req = { mandrill_events: '[{"event":"inbound","ts":1426249238}]' } console. log(Object. keys(req)[0]);

Can JSON data start with Array?

So, the answer to the question is still yes, JSON text can start with a square bracket (i.e. an array).

Does order of elements matter in JSON?

The JSON RFC (RFC 4627) says that order of object members does not matter.


2 Answers

Just dont loop and do something like that:

private static int FIRST_ELEMENT = 0;

public static void main(String[] args) {
    JSONArray json = new JSONArray("[{\"Hello1\":\"1\"},{\"Hello2\":\"2\"}]");

    if (json.length() > 0) {
        System.out.println("First: " + json.getJSONObject(FIRST_ELEMENT).toString());// parse the date instead of toString()
    }
}

iam not sure if this is exactly what you want ;)

like image 195
Yser Avatar answered Sep 24 '22 11:09

Yser


Get shorter answer for free!

JSONObject first =
  new JSONArray(
    "[{\"Key1\":\"Value1\"}]").
  getJSONObject(0);
like image 24
Zon Avatar answered Sep 23 '22 11:09

Zon