Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
simplejson/java-如何处理空数组/列表_Java_Simplejson - Fatal编程技术网

simplejson/java-如何处理空数组/列表

simplejson/java-如何处理空数组/列表,java,simplejson,Java,Simplejson,下面的java块处理simplejson和string/JSONArray转换。 我正在centos上运行jre 1.6 我的目标是,尝试找出如何将空数组作为输入文本处理,并转换为simplejson JSONArray 区块中的“s”表示测试应用程序中有效输入的样本 . . . import org.json.simple.*; String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; String s="[n

下面的java块处理simplejson和string/JSONArray转换。 我正在centos上运行jre 1.6

我的目标是,尝试找出如何将空数组作为输入文本处理,并转换为simplejson JSONArray

区块中的“s”表示测试应用程序中有效输入的样本

.
.
.
import org.json.simple.*;

    String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
    String s="[null]";

    Object objm=JSONValue.parse(s);
    JSONArray array=(JSONArray)objm;
    System.out.println(array.size();

//the above works as expected...
however, if i use a string of

    String s="[]";

i get an error:
    Exception in thread "main" java.lang.ClassCastException: 
    java.lang.String cannot be cast to org.json.simple.JSONArray
因此,我试图理解如何使用“[]”,而不必检查文本以转换为[]数组

有什么想法吗

谢谢

请检查一下:

if (!"[]".equals(s)) {
  Object objm=JSONValue.parse(s);
  JSONArray array=(JSONArray)objm;
  System.out.println(array.size();
} else {
  // ..
}
只需检查它:

if (!"[]".equals(s)) {
  Object objm=JSONValue.parse(s);
  JSONArray array=(JSONArray)objm;
  System.out.println(array.size();
} else {
  // ..
}