【发布时间】:2018-09-01 08:04:59
【问题描述】:
我可以读取一个简单的 json 文件,它有一个键值对,代码是
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class readconfig {
public static void main(String Args[]) throws JsonParseException, JsonMappingException, IOException{
// String inputjson = "{\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\"}";
ObjectMapper mapper= new ObjectMapper();
// readjson mp =mapper.readValue(inputjson, readjson.class);
readjson mp =mapper.readValue(new FileInputStream("sam.json"), readjson.class );
System.out.println(mp.getKey1());
}
}
import java.io.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class readjson {
public String k1;
public String k2;
public String k3;
public key1 key;
public key1 getKey() {
return key;
}
public void setKey(key1 key) {
this.key = key;
}
public String getKey1() {
return k1;
}
public void setKey1(String key1) {
this.k1 = key1;
}
public String getKey2() {
return k2;
}
public void setKey2(String key2) {
this.k2 = key2;
}
public String getKey3() {
return k3;
}
public void setKey3(String key3) {
this.k3 = key3;
}
}
问题是我的 json 文件很复杂 这是我的 json 文件,它是地图的地图,内部地图的值是一个列表
{
"key1":
{ "value1":
["listele1",
"listele2"
]
},
"key2":
{ "value2":
["listele1",
"listele2"
]
},
"key3":
{ "value3":
["listele1",
"listele2"
]
}
}
你能帮我修改列表中内部映射值的值,还可以反序列化 json 并将 json 作为对象获取
【问题讨论】:
标签: java json object jackson json-deserialization