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

Java提取JSON中industryIdentifiers的identifier值避免丢数求助

Fixing ISBN Identifier Extraction Without Losing Digits in Java

I see exactly what's going wrong here—your code has two critical mistakes that are causing digit loss and incorrect parsing:

  1. You’re treating items as a JSONObject, but it’s actually a JSONArray (look at the square brackets [] around it in your response).
  2. You’re calling getString(0) on the industryIdentifiers array, but each element in that array is a JSONObject (not a raw string), so you need to access the identifier field inside each object.

Correct Approach Using the org.json Library

Here’s the fixed code that properly extracts all identifiers without losing any digits (including leading zeros in ISBN_10):

import org.json.JSONArray;
import org.json.JSONObject;

public class IsbnExtractor {
    public static void main(String[] args) {
        String jsonResponse = "{ \"items\": [ { \"volumeInfo\": { \"industryIdentifiers\": [ { \"type\": \"ISBN_10\", \"identifier\": \"0080509576\" }, { \"type\": \"ISBN_13\", \"identifier\": \"9780080509570\" } ] } } ] }";
        
        // Parse the root JSON object
        JSONObject rootObj = new JSONObject(jsonResponse);
        
        // Get the items array (since items is an array, not a single object)
        JSONArray itemsArray = rootObj.getJSONArray("items");
        
        // Grab the first item in the array (adjust index if there are multiple items)
        JSONObject firstBookItem = itemsArray.getJSONObject(0);
        
        // Access the volumeInfo object
        JSONObject volumeInfo = firstBookItem.getJSONObject("volumeInfo");
        
        // Get the industryIdentifiers array
        JSONArray identifiersArray = volumeInfo.getJSONArray("industryIdentifiers");
        
        // Iterate through each identifier object to extract the value
        for (int i = 0; i < identifiersArray.length(); i++) {
            JSONObject identifierObj = identifiersArray.getJSONObject(i);
            String isbnType = identifierObj.getString("type");
            String isbnNumber = identifierObj.getString("identifier");
            System.out.println(isbnType + ": " + isbnNumber);
        }
    }
}

Why This Works

  • Proper array handling: We use getJSONArray("items") instead of getJSONObject("items") to correctly access the list of book items.
  • String retrieval: Using getString("identifier") ensures we pull the exact string value from each object, avoiding any conversion to numeric types that would drop leading zeros or truncate digits.

Alternative: Using Jackson Library (Object Mapping)

If you prefer a more type-safe approach with Jackson (a popular JSON processing library), here’s how to do it:

First, define simple POJO classes to map the JSON structure:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class BookResponse {
    @JsonProperty("items")
    private List<BookItem> items;

    // Getters and setters
    public List<BookItem> getItems() { return items; }
    public void setItems(List<BookItem> items) { this.items = items; }
}

class BookItem {
    @JsonProperty("volumeInfo")
    private VolumeInfo volumeInfo;

    // Getters and setters
    public VolumeInfo getVolumeInfo() { return volumeInfo; }
    public void setVolumeInfo(VolumeInfo volumeInfo) { this.volumeInfo = volumeInfo; }
}

class VolumeInfo {
    @JsonProperty("industryIdentifiers")
    private List<IndustryIdentifier> industryIdentifiers;

    // Getters and setters
    public List<IndustryIdentifier> getIndustryIdentifiers() { return industryIdentifiers; }
    public void setIndustryIdentifiers(List<IndustryIdentifier> industryIdentifiers) { this.industryIdentifiers = industryIdentifiers; }
}

class IndustryIdentifier {
    @JsonProperty("type")
    private String type;
    @JsonProperty("identifier")
    private String identifier;

    // Getters and setters
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getIdentifier() { return identifier; }
    public void setIdentifier(String identifier) { this.identifier = identifier; }
}

Then parse and extract the identifiers:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonIsbnExtractor {
    public static void main(String[] args) throws Exception {
        String jsonResponse = "{ \"items\": [ { \"volumeInfo\": { \"industryIdentifiers\": [ { \"type\": \"ISBN_10\", \"identifier\": \"0080509576\" }, { \"type\": \"ISBN_13\", \"identifier\": \"9780080509570\" } ] } } ] }";
        
        ObjectMapper objectMapper = new ObjectMapper();
        BookResponse response = objectMapper.readValue(jsonResponse, BookResponse.class);
        
        // Extract identifiers from the first book item
        List<IndustryIdentifier> identifiers = response.getItems().get(0).getVolumeInfo().getIndustryIdentifiers();
        for (IndustryIdentifier id : identifiers) {
            System.out.println(id.getType() + ": " + id.getIdentifier());
        }
    }
}

This approach maps JSON directly to Java objects, making your code more readable and less error-prone than manual parsing.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:32:51