当我尝试获取JSONArray的长度时,Eclipse总是给我这个编译错误:
对于JSONArray类型
,方法length()未定义
以下是代码:
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();
我不知道为什么会出现这个错误。这是怎么造成的,我该如何解决?
答案 0 :(得分:3)
javadoc here没有显示JSONArray的length()方法。因此错误。它确实有size(),那就是你所追求的吗?
答案 1 :(得分:0)
让它更清晰的例子: 假设personList是用户创建的类'Person'的arraylist,
$.ajax({
success:function (resultData) {
var resultListData = resultData.personList; // personList as a JSON Object got by Action response.
if(resultListData.length > 0){
// process results
$.each(resultListData, function (iter) {
alert(resultListData[iter].personFirstName);
}else{
alert("No Data to display");
}
}
});