Eclipse always gives me this compilation error when I try to get the length of a JSONArray:
the method length() is undefined for the type JSONArray
Here is the code:
import org.springframework.context.annotation.Scope;
import java.net.*;
import java.io.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import javax.inject.Named;
@Named("search")
@Scope("request")
public class Search {
private String query;
private String result;
private int num;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String getResult() {
return this.result;
}
public void setResult(String result) {
this.result = result;
}
public int getNum() {
return this.num;
}
public void setNum(int num) {
this.num = num;
}
public String send() {
try
{
//SEND REQUEST TO SOLR SERVER
URL url = new URL("http://localhost:8983/solr/select/?q="+this.query +"&version=2.2&start=0&rows=100&indent=on&wt=json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null)
{
this.result = this.result+str;
}
in.close();
//CONVERT RESULT TO OBJECT
this.result=this.result.substring(4);
JSONObject json = (JSONObject) JSONSerializer.toJSON(this.result);
JSONArray results = new JSONArray();
json = json.getJSONObject("response");
this.num = json.getInt("numFound");
results = json.getJSONArray("docs");
int num = results.length();
I have no idea why this error is popping up. How is this caused and how can I solve it?
The javadoc here doesn’t show a length() method for JSONArray. Hence the error. It does have size() though, is that what you’re after?