简体   繁体   English

JSON数组读取第一个元素?

[英]JSON array Read first element?

I got an JSON Array for my Twitch Following user checker. 我为我的Twitch提供了一个JSON数组跟随用户检查器。 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);

                    }
              }

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 ;) 我不确定这是不是你想要的;)

Get shorter answer for free! 免费获得更短的答案!

JSONObject first =
  new JSONArray(
    "[{\"Key1\":\"Value1\"}]").
  getJSONObject(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM