java - JSONArray 类型未定义方法 length()

标签 java

当我尝试获取 JSONArray 的长度时,Eclipse 总是给我这个编译错误:

the method length() is undefined for the type 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();

我不知道为什么会弹出这个错误。这是怎么引起的,我该如何解决?

最佳答案

javadoc here不显示 JSONArray 的 length() 方法。因此错误。不过它确实有 size(),这是您想要的吗?

关于java - JSONArray 类型未定义方法 length(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8400985/

相关文章:

java - 如何将 Camunda ExternalTask​​Client 绑定(bind)到本地端口?

java - Spring MVC 中的嵌套异常处理

java - 如何在 Hibernate 中添加来自字符串列表的多个 OR?

java - 更好的客户端/服务器套接字系统解决方案

java - 如果我需要从线程的可运行对象返回一个对象怎么办?

java - Android 相机预览未显示

java - 尝试使用 OpenGL 时,由于 NumberFormatException, Activity 崩溃

java - Future 和 Mono 的区别

用于只读数据库的 java ORM

java - ricorsive 方法中的字符串索引越界异常