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

Java解析指定JSON时出现Parsing JSON fails with not a JSONArray错误求助

Fixing "not a JSONArray" Error When Parsing Your JSON in Java

Hey there! Let's break down why you're hitting that "Parsing JSON fails with not a JSONArray" error and how to fix it. First, let's look at the JSON you're working with—this is the root of the issue:

{ 
  "data": { 
    "pageIDentifier":" nametitle", 
    "page": { 
      "pageID":"sports_league_member", 
      "platform":" www", 
      "activityType":" ent", 
      "businessUnit":" ent", 
      "productLOB":" ent", 
      "productOffered":" abc", 
      "productQualifier":"abc:a bc", 
      "flowType":"sports_com", 
      "pageDesc":" desc", 
      "attributes": { 
        "pageType":" www", 
        "host":"finaluser", 
        "appId":"SportsAppID_user", 
        "daEnvironment":"releaser", 
        "jvm":" ent_logon_01", 
        "xCKey":"SportsAppID_use..." 
      } 
    } 
  }
}

Why the Error Happens

Notice your JSON starts with curly braces {—that means it's a JSON Object, not a JSON Array (which uses square brackets []). The error pops up because your code is trying to parse this object as if it were an array (like calling new JSONArray(jsonString) or mapping it to a Java List/array type).

How to Fix It

Let's cover solutions for the most common JSON libraries used in Java:

1. Using org.json Library

If you're using the basic org.json package, parse the top-level structure as a JSONObject, then navigate down through nested objects:

import org.json.JSONObject;

public class JsonParsingExample {
    public static void main(String[] args) {
        String jsonInput = "{ \"data\": { \"pageIDentifier\":\" nametitle\", \"page\": { \"pageID\":\"sports_league_member\", \"platform\":\" www\", \"activityType\":\" ent\", \"businessUnit\":\" ent\", \"productLOB\":\" ent\", \"productOffered\":\" abc\", \"productQualifier\":\"abc:a bc\", \"flowType\":\"sports_com\", \"pageDesc\":\" desc\", \"attributes\": { \"pageType\":\" www\", \"host\":\"finaluser\", \"appId\":\"SportsAppID_user\", \"daEnvironment\":\"releaser\", \"jvm\":\" ent_logon_01\", \"xCKey\":\"SportsAppID_use...\" } } }";
        
        // Parse top-level object (not array!)
        JSONObject topLevelObj = new JSONObject(jsonInput);
        
        // Drill down to nested objects
        JSONObject dataObj = topLevelObj.getJSONObject("data");
        JSONObject pageObj = dataObj.getJSONObject("page");
        JSONObject attributesObj = pageObj.getJSONObject("attributes");
        
        // Extract values you need
        String pageID = pageObj.getString("pageID");
        String appId = attributesObj.getString("appId");
        
        System.out.println("Page ID: " + pageID);
        System.out.println("App ID: " + appId);
    }
}

2. Using Jackson Library

For Jackson, create POJO classes that mirror your JSON structure, then deserialize into those classes:

First, define the POJOs:

// Top-level response class
public class TopLevelResponse {
    private Data data;
    
    // Getters and Setters
    public Data getData() { return data; }
    public void setData(Data data) { this.data = data; }
}

class Data {
    private String pageIDentifier;
    private Page page;
    
    // Getters and Setters
    public String getPageIDentifier() { return pageIDentifier; }
    public void setPageIDentifier(String pageIDentifier) { this.pageIDentifier = pageIDentifier; }
    public Page getPage() { return page; }
    public void setPage(Page page) { this.page = page; }
}

class Page {
    private String pageID;
    private String platform;
    private String activityType;
    // Add other fields as needed
    private Attributes attributes;
    
    // Getters and Setters for all fields
    public String getPageID() { return pageID; }
    public void setPageID(String pageID) { this.pageID = pageID; }
    public Attributes getAttributes() { return attributes; }
    public void setAttributes(Attributes attributes) { this.attributes = attributes; }
}

class Attributes {
    private String pageType;
    private String host;
    private String appId;
    // Add other fields as needed
    
    // Getters and Setters
    public String getAppId() { return appId; }
    public void setAppId(String appId) { this.appId = appId; }
}

Then parse the JSON:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonParsingExample {
    public static void main(String[] args) throws Exception {
        String jsonInput = "{ \"data\": { \"pageIDentifier\":\" nametitle\", \"page\": { \"pageID\":\"sports_league_member\", \"platform\":\" www\", \"activityType\":\" ent\", \"businessUnit\":\" ent\", \"productLOB\":\" ent\", \"productOffered\":\" abc\", \"productQualifier\":\"abc:a bc\", \"flowType\":\"sports_com\", \"pageDesc\":\" desc\", \"attributes\": { \"pageType\":\" www\", \"host\":\"finaluser\", \"appId\":\"SportsAppID_user\", \"daEnvironment\":\"releaser\", \"jvm\":\" ent_logon_01\", \"xCKey\":\"SportsAppID_use...\" } } }";
        
        ObjectMapper mapper = new ObjectMapper();
        TopLevelResponse response = mapper.readValue(jsonInput, TopLevelResponse.class);
        
        // Access the parsed data
        String pageID = response.getData().getPage().getPageID();
        System.out.println("Page ID: " + pageID);
    }
}

3. Using Gson Library

Gson works similarly to Jackson—define matching POJOs and deserialize:

import com.google.gson.Gson;

public class GsonParsingExample {
    public static void main(String[] args) {
        String jsonInput = "{ \"data\": { \"pageIDentifier\":\" nametitle\", \"page\": { \"pageID\":\"sports_league_member\", \"platform\":\" www\", \"activityType\":\" ent\", \"businessUnit\":\" ent\", \"productLOB\":\" ent\", \"productOffered\":\" abc\", \"productQualifier\":\"abc:a bc\", \"flowType\":\"sports_com\", \"pageDesc\":\" desc\", \"attributes\": { \"pageType\":\" www\", \"host\":\"finaluser\", \"appId\":\"SportsAppID_user\", \"daEnvironment\":\"releaser\", \"jvm\":\" ent_logon_01\", \"xCKey\":\"SportsAppID_use...\" } } }";
        
        Gson gson = new Gson();
        TopLevelResponse response = gson.fromJson(jsonInput, TopLevelResponse.class);
        
        String appId = response.getData().getPage().getAttributes().getAppId();
        System.out.println("App ID: " + appId);
    }
}

Key Takeaway

Always match your parsing code to the actual JSON structure:

  • Use JSONObject (or a top-level POJO) when the JSON starts with {
  • Use JSONArray (or a List/array type) only when the JSON starts with []

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:53:05