javaでjsonファイルを読み込むためにJacksonを使ってみました。
jsonの形式自体あまり理解できていませんが、配列を使う2つのケースを読み込みを試しました。
形式に関しては、ここに色々なパターンが書いてあり、参考にしています。
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html
POJOを使う読み込み方はいろいろなサイトを参照。
パターン1
・sample1.json
{
"users":
[
{
"name": "taro",
"country": "japan",
"age": 12
},
{
"name": "jack",
"country": "us",
"age": 20
}
]
}
・JsonTest1.java
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonTest1 {
public static void main(String args[]) throws JsonParseException,
JsonMappingException, IOException {
JsonTest1 converter = new JsonTest1();
converter.parse("sample1.json");
}
public void parse(String fname) throws JsonParseException,
JsonMappingException, IOException {
UserList users = new ObjectMapper().readValue(new File(fname),
UserList.class);
for (User user : users.getUsers()) {
System.out.println(user.getName());
System.out.println(user.getCountry());
System.out.println(user.getAge());
}
}
public static class UserList {
private List<User> users;
public UserList() {
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
public static class User {
private String name;
private String country;
private int age;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
パターン2
・sample2.json
[
{
"pair": "USDEUR",
"price": 1.3588,
"change": -0.0088
},
{
"pair": "USDJPY",
"price": 102.4250,
"change": 0.0049
}
]
・JsonTest1.java
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonTest2 {
public static void main(String args[]) throws JsonParseException,
JsonMappingException, IOException {
JsonTest2 converter = new JsonTest2();
converter.parse("sample2.json");
}
public void parse(String fname) throws JsonParseException,
JsonMappingException, IOException {
List<Currency> pairs = new ObjectMapper().readValue(new File(fname),
new TypeReference<List<Currency>>() {
});
for (Currency c : pairs) {
System.out.println(c.toString());
}
}
public static class Currency {
private String pair;
private double price;
private double change;
public Currency() {
}
public String getPair() {
return pair;
}
public void setParr(String parr) {
this.pair = parr;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getChange() {
return change;
}
public void setChange(double change) {
this.change = change;
}
@Override
public String toString() {
return pair+":"+price+"("+change+"%)";
}
}
}