I'm doing a project where I just get some JSON data from an API and I display it in a interface. However, when I try to parse the JSON and put it into an array it with a for loop, it turns the data into null.
When I put in the first String array, it's fine, but every time I put in another, all other arrays become filled with null
I don't understand why this is happening, maybe its related to how I'm parsing the JSON?
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
public class HTTP {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String AUTHENTICATION = "OrpLanM2WUiEppeeg6CW";
public static int index;
public static String output = "";
String[] result;
public static String[][] resultArray;
public static JSONArray docs;
public static void parseJSON(Object file) {
// In java JSONObject is used to create JSON object
JSONObject json = (JSONObject) file;
System.out.println(json);
docs = (JSONArray) json.get("docs");
System.out.println(docs);
if (index <= -1){
System.out.println(docs.size());
for (int i = 0; i < docs.size(); i++) {
JSONObject o = (JSONObject) docs.get(i);
String objString = o.toString();
String[] result = objString.split("[:,{}]");
System.out.println("result :"+Arrays.toString(result));
String[][] resultArray = new String[docs.size()][result.length];
for(int j = 0; j< result.length; j++){
resultArray[i][j] = result[j];
}
//resultArray[i] = result;
System.out.println(Arrays.toString(resultArray[i]));
HTTP.resultArray = resultArray;
//System.out.println(Arrays.toString(HTTP.resultArray[i]));
if(i>0) {
System.out.println(Arrays.toString(resultArray[i-1]));
//System.out.println(Arrays.toString(HTTP.resultArray[i-1]));
}
}
System.out.println(Arrays.toString(HTTP.resultArray[0]));
}
else {
JSONObject o = (JSONObject) docs.get(index);
String objString = o.toString();
String[] result = objString.split("[:,{}]");
System.out.println("result :"+result);
String[][] resultArray = new String[docs.size()][result.length];
resultArray[index] = result;
HTTP.resultArray = resultArray;
}
}
public static void sendGET(String url) throws IOException {
URL obj = new URL("https://the-one-api.dev/v2/"+url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Authorization", "Bearer " + AUTHENTICATION);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
output = response.toString();
Object file = JSONValue.parse(output);
parseJSON(file);
} else {
System.out.println("GET request not worked");
}
}
}
The problem is that you are not only re-creating your array in every loop cycle, the static variable from above is not even used.
String[][] resultArray = new String[docs.size()][result.length];
By doing that, you delete it's values. I think your problem is that you don't know how large the array at this point needs to be. I don't know if this is what you need, but you could try doing this:
(I'm gonna pretend you want to use your static variable)
You have your array here:
public static String[][] resultArray;
Then you initialize it like this:
docs.size()
like abovedocs.size()
) resultArray = new String[docs.size()][10];
Then in your loop, you do this:
resultArray[i] = result;
If you do this, not only your data should not be deleted, but you also don't need the loop where you copy the data from your result array ( result
) to your actual array ( resultArray
)
Also, in your else block, you just leave out the String[][]
in the line
String[][] resultArray = new String[docs.size()][result.length];
By doing that, you should be able to call your static variable from above and not creating a new one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.