从JSON文件中的JSON数组读取对象数组(使用Jackson)

时间:2018-08-03 16:30:10

标签: java json jackson

我正在尝试从json文件中的JSON数组读取对象数组。读取对象内部的数组(让我们将其称为根对象)并不确定该怎么做已经有一段时间了。底部的问题。

拳头类:(包含主要方法)

package Shopping;
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.parser.ParseException;

public class Game {        

public static void main(String[] args) throws IOException, 
ParseException{ 

    File file = new File("shopping.json");       
    ObjectMapper mapper = new ObjectMapper();

        Main m = mapper.readValue(file, Main.class);
        System.out.println(m.getDate());

        ShoppingList[] list = mapper.readValue(file, 
        ShoppingList[].class);
        System.out.println(list.toString());

    }
}

第二堂课

package Shopping;
import org.codehaus.jackson.annotate.JsonProperty;

public class Main {
private String date; 
private String name;
private String Store;

private ShoppingList List;

// BELOW THE FIELDS ARE THE GETTERS AND SETTERS FOR THESE FIELDS
}

第三堂课:

package Shopping;

import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.json.simple.parser.ParseException;

public class ShoppingList {

private String name;
private int amount;

// AGAIN BELOW THESE FIELDS ARE THE GETTERS AND SETTERS FOR THESE FIELDS
}

这是保存JSON数据的JSON文件:(Shopping.json)

{
  "date": "2016-07-14",
  "name": "Candice",
  "Store": "aStore",
  "Food": [
    {
      "amount": 6,
      "name": "eggs"
    },
    {
      "amount": 1,
      "name": "Chicken"
    },
    {
      "amount": 5,
      "name": "Bananas"
    },
    {
      "amount": 1,
      "name": "Pasta"
    }
  ]
}

当我尝试运行主要方法时,出现以下错误消息:

run:
Exception in thread "main" 
org.codehaus.jackson.map.JsonMappingException: Can not deserialize 
instance of Shopping.ShoppingList out of START_ARRAY token
at [Source: Shopping.json; line: 6, column: 26] (through reference chain: 
Shopping.Main["Food"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeUsingCreator(BeanDeserializer.java:560)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:366)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:230)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:334)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:495)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1349)
at shopping.Shopping.main(Shopping.java:31) 
/Users/Me/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

任何人都可以请我解释为什么我会收到此错误,并且有人可以解决此问题,以便我可以读取数组以返回其中的项目。任何帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

Pojo课堂中的问题。 试试这个:

转换器:

There is no dynamic section in this file.

主类:

String json= "{\n" +
            "  \"date\": \"2016-07-14\",\n" +
            "  \"name\": \"Candice\",\n" +
            "  \"Store\": \"aStore\",\n" +
            "  \"Food\": [\n" +
            "    {\n" +
            "      \"amount\": 6,\n" +
            "      \"name\": \"eggs\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"amount\": 1,\n" +
            "      \"name\": \"Chicken\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"amount\": 5,\n" +
            "      \"name\": \"Bananas\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"amount\": 1,\n" +
            "      \"name\": \"Pasta\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";
Main main = new ObjectMapper().readValue(json,Main.class);

购物清单:

package Shopping;
import org.codehaus.jackson.annotate.JsonProperty;

public class Main {
private String date;
private String name;
@JsonProperty("Store")
private String store;

@JsonProperty("Food")
private List<ShoppingList> Food;

// BELOW THE FIELDS ARE THE GETTERS AND SETTERS FOR THESE FIELDS
}