You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Processing解析JSON数组报错:JSONArray text must start with '['

解决Processing解析JSON数组时的"JSONArray text must start with '['"错误

错误信息

java.lang.RuntimeException: A JSONArray text must start with '[
    at processing.data.JSONArray.<init>(JSONArray.java:140)
    at processing.data.JSONArray.<init>(JSONArray.java:126)
    at processing.core.PApplet.parseJSONArray(PApplet.java:5494)
    at sketch_230108a.fileSelected(sketch_230108a.java:31)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at processing.core.PApplet.selectCallback(PApplet.java:5966)
    at processing.awt.ShimAWT.selectImpl(ShimAWT.java:835)
    at processing.awt.ShimAWT.lambda$selectInput$0(ShimAWT.java:742)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

测试代码

PrintWriter output;  
String data = "[ \"Capra hircus\", \"Panthera pardus\", \""+"90"+"\" ]";

void setup() {
  output = createWriter("positions.txt"); 
  output.print(data);
  output.flush();
  output.close();
  selectInput("Select a file to process:", "fileSelected");
}
 
void fileSelected(File selection) {
   JSONArray json = parseJSONArray(selection.toString());
  if (json == null) {
    println("JSONArray could not be parsed");
  } else {
    String species = json.getString(0);
    println(species);
  }
}

问题分析

核心错误原因:parseJSONArray(selection.toString())这行代码传入的是文件路径字符串(比如/xxx/positions.txt),而非文件内的JSON内容。Processing的parseJSONArray()方法要求传入符合格式的JSON文本,路径字符串显然不是合法的JSON数组,因此触发"JSONArray text must start with '['"错误。

修正后的代码

PrintWriter output;  
String data = "[ \"Capra hircus\", \"Panthera pardus\", \""+"90"+"\" ]";

void setup() {
  output = createWriter("positions.txt"); 
  output.print(data);
  output.flush();
  output.close();
  selectInput("Select a file to process:", "fileSelected");
}
 
void fileSelected(File selection) {
  // 读取文件所有行并拼接成完整文本
  String fileContent = loadStrings(selection).join("\n");
  JSONArray json = parseJSONArray(fileContent);
  
  if (json == null) {
    println("JSONArray could not be parsed");
  } else {
    String species = json.getString(0);
    println(species); // 输出结果:Capra hircus
  }
}

修正说明

  • 使用loadStrings(selection)读取目标文件的所有内容,返回一个字符串数组
  • 通过join("\n")将数组拼接为完整的文本字符串,确保JSON结构的完整性
  • 将拼接后的文本传入parseJSONArray(),即可正确解析出JSON数组并获取目标内容

内容的提问来源于stack exchange,提问作者Rose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:15:36