使用Jackson解析JSON无法提取props字段,求Java POJO修改方案
Hey there! The problem you're facing is straightforward: your current Attrs POJO class doesn't include the props field present in your JSON structure. Jackson can only map fields that exist in both the JSON and your POJO, so we just need to add that field, plus fix a couple of minor type mismatches to ensure smooth parsing.
Step 1: Add the props Field to the Attrs Class
In your JSON, the props field is an array of strings (e.g., ["Delhi", "Mumbai", ...]). You'll need to add this field to your Attrs class, along with its getter, setter, and update the toString() method to include it properly.
Here's the modified Attrs.java:
import java.util.Arrays; public class Attrs { private Configs configs; private String id; private String attr; // New field to map the JSON "props" array private String[] props; public Configs getConfigs() { return configs; } public void setConfigs(Configs configs) { this.configs = configs; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAttr() { return attr; } public void setAttr(String attr) { this.attr = attr; } // Getter and setter for the new props field public String[] getProps() { return props; } public void setProps(String[] props) { this.props = props; } @Override public String toString() { return "Attrs [configs=" + configs + ", id=" + id + ", attr=" + attr + ", props=" + Arrays.toString(props) + "]"; } }
Note: I used Arrays.toString(props) in the toString() method so the array content prints clearly instead of just showing an object reference.
Step 2: Fix the id Type Mismatch in Sets Class
Looking at your JSON, the id field under sets is a number ("id": 2), but your Sets class defines it as a String. This can trigger Jackson parsing errors. Change it to an Integer (or primitive int if you prefer):
Modified Sets.java snippet:
import java.util.Arrays; public class Sets { // Changed from String to Integer to match JSON's numeric id private Integer id; private Attrs[] attrs; private String set; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Attrs[] getAttrs() { return attrs; } public void setAttrs(Attrs[] attrs) { this.attrs = attrs; } public String getSet() { return set; } public void setSet(String set) { this.set = set; } @Override public String toString() { return "Sets [id=" + id + ", attrs=" + Arrays.toString(attrs) + ", set=" + set + "]"; } }
Optional: Use List Instead of Arrays for Flexibility
While arrays work, using List (e.g., List<String>) is more flexible for Jackson parsing, especially if the JSON array size varies. Here's how you could adjust the Attrs class for this:
private List<String> props; public List<String> getProps() { return props; } public void setProps(List<String> props) { this.props = props; }
Jackson will automatically map the JSON array to a List without any extra configuration.
Testing the Fix
Once you've made these changes, you can test with a simple Jackson parsing snippet:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class ParserTest { public static void main(String[] args) { String yourJsonString = "{ /* your full JSON here */ }"; ObjectMapper objectMapper = new ObjectMapper(); try { Root root = objectMapper.readValue(yourJsonString, Root.class); // Access the props field String[] props = root.getIActs()[0].getSets()[0].getAttrs()[3].getProps(); System.out.println("Extracted props: " + Arrays.toString(props)); } catch (JsonProcessingException e) { e.printStackTrace(); } } } // Add this root class to wrap the top-level iActs array class Root { private IActs[] iActs; public IActs[] getIActs() { return iActs; } public void setIActs(IActs[] iActs) { this.iActs = iActs; } }
These changes should let you successfully extract the props field from your JSON using Jackson.
内容的提问来源于stack exchange,提问作者Snehal Gupta

