标签:text trace print amt rgs row bool word lin
public
class
JacksonTester {
public
static
void
main(String args[]){
JacksonTester tester =
new
JacksonTester();
try
{
JsonFactory jasonFactory =
new
JsonFactory();
JsonGenerator jsonGenerator = jasonFactory.createJsonGenerator(
new
File(
"student.json"
), JsonEncoding.UTF8);
// {
jsonGenerator.writeStartObject();
// "name" : "Mahesh Kumar"
jsonGenerator.writeStringField(
"name"
,
"Mahesh Kumar"
);
// "age" : 21
jsonGenerator.writeNumberField(
"age"
,
21
);
// "verified" : false
jsonGenerator.writeBooleanField(
"verified"
,
false
);
// "marks" : [100, 90, 85]
jsonGenerator.writeFieldName(
"marks"
);
// [
jsonGenerator.writeStartArray();
// 100, 90, 85
jsonGenerator.writeNumber(
100
);
jsonGenerator.writeNumber(
90
);
jsonGenerator.writeNumber(
85
);
// ]
jsonGenerator.writeEndArray();
// }
jsonGenerator.writeEndObject();
jsonGenerator.close();
//result student.json
//{
// "name":"Mahesh Kumar",
// "age":21,
// "verified":false,
// "marks":[100,90,85]
//}
ObjectMapper mapper =
new
ObjectMapper();
Map<String,Object> dataMap = mapper.readValue(
new
File(
"student.json"
), Map.
class
);
System.out.println(dataMap.get(
"name"
));
System.out.println(dataMap.get(
"age"
));
System.out.println(dataMap.get(
"verified"
));
System.out.println(dataMap.get(
"marks"
));
}
catch
(JsonParseException e) {
e.printStackTrace();
}
catch
(JsonMappingException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
标签:text trace print amt rgs row bool word lin
原文地址:https://www.cnblogs.com/dogdogwang/p/10792204.html