使用jackson将数据从json写入java对象

时间:2014-04-18 17:10:51

标签: java json jackson

我一直在使用jackson库从输入流中读取数据到java对象。

代码如下:

json是字符串,其中数据采用JSON

的形式
 BufferedReader br = new BufferedReader(newInputStreamReader(request.getInputStream()));

 String json = "";

 json = br.readLine();
ObjectMapper mapper = new ObjectMapper():
Position position = mapper.readValue(json, Position.class);

其中Position是普通的getter setter类。现在,当我使用

将数据写入文件时
FileWriter writer = new FileWriter("E://out.txt");
writer.write(position.getMobile());
writer.close();

它不会在文本文件上写入任何数据,也不会创建文件。

还有一件事,如果我只写json,就像文件一样,没关系。我的意思是它是以JSON的形式将json字符串写入文件。

1 个答案:

答案 0 :(得分:0)

我做了一个例子,它对我来说很好。

域类:位置

package de.professional_webworkx.jackson.jacksondemo.domain;

import java.io.Serializable;
import java.util.Random;

public class Position implements Serializable {

private Long id;
private double lat;
private double lon;

protected Position() {}

public Position(final double lat, final double lon) {
    this.id     = generateId();
    this.lat    = lat;
    this.lon    = lon;
}

private Long generateId() {
    Random random = new Random(System.currentTimeMillis());
    return random.nextLong();
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLon() {
    return lon;
}

public void setLon(double lon) {
    this.lon = lon;
}

}

启动应用

package de.professional_webworkx.jackson.jacksondemo;

import de.professional_webworkx.jackson.jacksondemo.domain.Person;
import de.professional_webworkx.jackson.jacksondemo.domain.Position;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jackson.map.ObjectMapper;

public class App {

    public static final File FILENAME = new File("position.json");

    public static final void main(String[] args) {
        LocalDate birthday = LocalDate.of(1980, 1, 1);
        Person person = new Person("Patrick", "Ott", "patrick.ott@professional-webworkx.de");

        Position position = new Position(48.113345, 11.273829);

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(FILENAME, position);
            // here i read the position from the *.json file
            Position readValue = mapper.readValue(FILENAME, Position.class);
            System.out.println(readValue.getLat() + "N, " + readValue.getLon() + "E" );
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

如果您需要pom.xml的依赖项

<dependencies>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.4</version>
            <type>jar</type>
        </dependency>
    </dependencies>

结果如下:

{"id":5799095657619062507,"lat":48.113345,"lon":11.273829}