2011-12-06 32 views
3

的Eclipse總是給我這個編譯錯誤,當我試圖得到一個JSONArray的長度)方法長度()是未定義的類型JSONArray

方法長度(未定義的類型JSONArray

下面是代碼:

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

它會幫助,如果你能後的樣本JSON對象了。 –

回答

3

javadoc here不顯示JSONArray的length()方法。因此錯誤。它確實有尺寸(),但是你在做什麼?

+0

哈哈謝謝!我覺得有點愚蠢,因爲我剛剛搜索了「class jsonarray」,並發現了這個javadoc [here](http://www.json.org/javadoc/org/json/JSONArray.html),我在找的東西是方法length()。 – user871784

0

實例,使之更加明確: 假設personList是用戶創建的類「人」的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"); 
} 
} 
}); 
相關問題