Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jackson通过json文件的键选择值_Java_Jackson - Fatal编程技术网

Java 使用jackson通过json文件的键选择值

Java 使用jackson通过json文件的键选择值,java,jackson,Java,Jackson,这可能是整个网站上最愚蠢的问题。我不熟悉Java和JSON,需要帮助。我正在使用API Jackson。程序接收一个JSON文件。从中我需要得到: 20岁至30岁的人群名单,按姓名排序 独特的城市名单 年龄间隔为0-10、11-20、21-30等的人数 目前,我已经学会了如何将json文件转换为java列表 import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.Jso

这可能是整个网站上最愚蠢的问题。我不熟悉Java和JSON,需要帮助。我正在使用API Jackson。程序接收一个JSON文件。从中我需要得到:

  • 20岁至30岁的人群名单,按姓名排序
  • 独特的城市名单
  • 年龄间隔为0-10、11-20、21-30等的人数
  • 目前,我已经学会了如何将json文件转换为java列表

    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.io.IOException;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
    
        ObjectMapper mapper = new ObjectMapper();
    
        try {
            List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\data.json").toFile(), Data[].class));
    
    
            System.out.println(data);
    
    
            } catch (JsonParseException jsonParseException) {
            jsonParseException.printStackTrace();
        } catch (JsonMappingException jsonMappingException) {
            jsonMappingException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    
    }
    }
    
    Json文件如下所示

    [
        {
            "id": "1",
            "firstName": "Lesley",
            "lastName": "Bryan",
            "dateOfBirth": "11/28/61",
            "city": "Southampton–Portsmouth"
        },
        {
            "id": "2",
            "firstName": "Edward",
            "lastName": "Houston",
            "dateOfBirth": "10/5/92",
            "city": "Southampton–Portsmouth"
        },
        {
            "id": "3",
            "firstName": "Donald",
            "lastName": "Ross",
            "dateOfBirth": "12/10/79",
            "city": "Glasgow"
        },
        {
            "id": "4",
            "firstName": "Peter",
            "lastName": "Kelly",
            "dateOfBirth": "3/17/04",
            "city": "Birmingham–Wolverhampton"
        },
        {
            "id": "5",
            "firstName": "Anthony",
            "lastName": "McKinney",
            "dateOfBirth": "3/6/68",
            "city": "Liverpool"
        },
        {
            "id": "6",
            "firstName": "David",
            "lastName": "Stewart",
            "dateOfBirth": "4/11/73",
            "city": "Leeds–Bradford"
        },
        {
            "id": "7",
            "firstName": "Christopher",
            "lastName": "Austin",
            "dateOfBirth": "12/28/74",
            "city": "Birmingham–Wolverhampton"
        },
        {
            "id": "8",
            "firstName": "Alvin",
            "lastName": "Hodge",
            "dateOfBirth": "11/25/58",
            "city": "Newcastle upon Tyne–Sunderland"
        },
        {
            "id": "9",
            "firstName": "Gerald",
            "lastName": "Higgins",
            "dateOfBirth": "6/28/55",
            "city": "Liverpool"
        },
        {
            "id": "10",
            "firstName": "Amos",
            "lastName": "Owens",
            "dateOfBirth": "1/16/01",
            "city": "Manchester-Salford"
        },
        {
            "id": "11",
            "firstName": "Christian",
            "lastName": "Bishop",
            "dateOfBirth": "11/14/50",
            "city": "Nottingham"
        },
        {
            "id": "12",
            "firstName": "Robert",
            "lastName": "Caldwell",
            "dateOfBirth": "12/8/80",
            "city": "Manchester-Salford"
        },
        {
            "id": "13",
            "firstName": "Brian",
            "lastName": "Heath",
            "dateOfBirth": "9/23/02",
            "city": "Newcastle upon Tyne–Sunderland"
        },
        {
            "id": "14",
            "firstName": "Mark",
            "lastName": "Anthony",
            "dateOfBirth": "1/8/92",
            "city": "London"
        },
        {
            "id": "15",
            "firstName": "Mark",
            "lastName": "Watson",
            "dateOfBirth": "7/27/91",
            "city": "Nottingham"
        },
        {
            "id": "16",
            "firstName": "Charles",
            "lastName": "Stafford",
            "dateOfBirth": "1/26/90",
            "city": "Birmingham–Wolverhampton"
        },
        {
            "id": "17",
            "firstName": "Steven",
            "lastName": "Merritt",
            "dateOfBirth": "12/4/63",
            "city": "Leeds–Bradford"
        },
        {
            "id": "18",
            "firstName": "John",
            "lastName": "Holmes",
            "dateOfBirth": "4/22/52",
            "city": "Southampton–Portsmouth"
        },
        {
            "id": "19",
            "firstName": "Mervin",
            "lastName": "Lewis",
            "dateOfBirth": "10/27/95",
            "city": "Birmingham–Wolverhampton"
        },
        {
            "id": "20",
            "firstName": "Peter",
            "lastName": "Marsh",
            "dateOfBirth": "12/10/63",
            "city": "Glasgow"
        },
        {
            "id": "21",
            "firstName": "Piers",
            "lastName": "Harrington",
            "dateOfBirth": "4/27/85",
            "city": "London"
        },
        {
            "id": "22",
            "firstName": "Matthew",
            "lastName": "O’Brien’",
            "dateOfBirth": "1/19/59",
            "city": "Manchester-Salford"
        }
    ]
    
    请告诉我完成任务的顺序。我再次为这么愚蠢的信息道歉。
    注意:json文件中的所有姓名、姓氏和日期都是随机获得的。

    首先,您需要更新

    数据
    类,以便于处理年龄和年龄组:

  • dateOfBirth
    的类型更改为
    LocalDate
    ,更新constructor/getter/provide custom setter以处理20世纪的日期
  • 添加以年和年龄组作为字符串获取年龄的方法:
  • 更新
    toString
    以打印年龄
  • 类数据{
    // ...
    @JsonFormat(shape=JsonFormat.shape.STRING,pattern=“M/d/yy”)
    私有本地出生日期;
    public LocalDate getDateOfBirth(){
    出生返回日期;
    }
    公共无效setDateOfBirth(LocalDate dob){
    if(dob.isAfter(LocalDate.now()){
    出生日期=出生日期(100年);
    }
    this.dateOfBirth=dob;
    }
    @杰索尼奥雷
    公共整数getAge(){
    返回期间.between(dateOfBirth,LocalDate.now()).getYears();
    }
    @杰索尼奥雷
    公共字符串getAgeGroup(){
    int age=getAge();
    if(年龄<11岁){
    返回“0..10”;
    }
    报税表(年龄/10*10+1)+.“+((年龄/10+1)*10);
    }
    @凌驾
    公共字符串toString(){
    返回“[id=“+id+”,age=“+getAge()+”,firstName=“+firstName+”,lastName=“+lastName+”,dateOfBirth=“+dateOfBirth+”,city=“+city+”]”;
    }
    }
    
    然后,您可以从输入列表中获取所需的数据子集:

    //按姓/名对20到30岁的人进行排序
    列表年龄20到30=数据
    .stream()
    .filter(p->p.getAge()>=20&&p.getAge()<30)
    .sorted(Comparator.comparing(p->p.getLastName()+“”+p.getFirstName())
    .collect(Collectors.toList());
    年龄20到30.forEach(System.out::println);
    //获取唯一城市的排序列表(使用TreeSet作为排序集)
    设置城市=数据
    .stream()
    .map(数据::getCity)
    .collect(收集器.toCollection(TreeSet::new));
    cities.forEach(System.out::println);
    //按年龄组获取统计数据
    地图字节=数据
    .stream()
    .collect(Collectors.groupingBy(Data::getAgeGroup,TreeMap::new,Collectors.counting());
    System.out.println(byAges);
    
    输出

    [id = 14, age=28, firstName = Mark, lastName = Anthony, dateOfBirth = 1992-01-08, city = London]
    [id = 2, age=27, firstName = Edward, lastName = Houston, dateOfBirth = 1992-10-05, city = Southampton–Portsmouth]
    [id = 19, age=24, firstName = Mervin, lastName = Lewis, dateOfBirth = 1995-10-27, city = Birmingham–Wolverhampton]
    [id = 15, age=29, firstName = Mark, lastName = Watson, dateOfBirth = 1991-07-27, city = Nottingham]
    
    Birmingham–Wolverhampton
    Glasgow
    Leeds–Bradford
    Liverpool
    London
    Manchester-Salford
    Newcastle upon Tyne–Sunderland
    Nottingham
    Southampton–Portsmouth
    
    {11..20=3, 21..30=4, 31..40=3, 41..50=3, 51..60=4, 61..70=5}
    

    无法构造
    com.testsample.Data
    (不存在与默认构造函数类似的创建者):无法从[Source:(文件);第3行,第3列](通过引用链:java.lang.Object[][0])处的对象值(无委托或基于属性的创建者)反序列化JSON文件的结构似乎存在问题。我仔细检查了上面作为字符串提供的数据在文件中的解析,结果很好。
    [id = 14, age=28, firstName = Mark, lastName = Anthony, dateOfBirth = 1992-01-08, city = London]
    [id = 2, age=27, firstName = Edward, lastName = Houston, dateOfBirth = 1992-10-05, city = Southampton–Portsmouth]
    [id = 19, age=24, firstName = Mervin, lastName = Lewis, dateOfBirth = 1995-10-27, city = Birmingham–Wolverhampton]
    [id = 15, age=29, firstName = Mark, lastName = Watson, dateOfBirth = 1991-07-27, city = Nottingham]
    
    Birmingham–Wolverhampton
    Glasgow
    Leeds–Bradford
    Liverpool
    London
    Manchester-Salford
    Newcastle upon Tyne–Sunderland
    Nottingham
    Southampton–Portsmouth
    
    {11..20=3, 21..30=4, 31..40=3, 41..50=3, 51..60=4, 61..70=5}