用于解析JSON的简单Spring代码 [英] Simple Spring code to parse JSON
问题描述
只获取名称:NULL
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args []){
RestTemplate restTemplate = new RestTemplate();
Computer [] computer = restTemplate.getForObject(http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505 \",Computer [] .class);
System.out.println(Name:+ computer [0] .getName());
}
}
这是简单的计算机课程。
package hello;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Computer {
private String hostname;
public String getName(){
return hostname;
}
public String toString(){
returnComputer [hostname =+ hostname +];
}
}
经过测试一些观察结果:
-
如果你不这样做的话......
如果你不这样做的话......
我非常了解 Spring
框架
,这没关系。要序列化/反序列化
JSON
可以通过多种方式完成。
我看过你的 JSON
包含一些保留
字样,如类
或 string
。为了处理这个问题,您需要使用 @JsonProperty
注释(稍后您将在 Computer.java $ c $中看到它c>和其他人)
。我还使用此注释来声明 JSON
属性中包含下划线的一些变量
(请记住, DTO的
[beans]
必须具有相同的结构
, datatypes
并使用相同的 JSON
属性名称。)
解决有关 RestTemplate
对象
的问题(我不确定如果你在使用计算机[] .class
)时采用正确的方法。作为建议,请考虑关注/检查此代码
代码段: https:// stackoverflow.com/a/6349488/1178686
- 这只是个人的:如果您认为此答案符合您的要求,请不要忘记单击答案旁边的复选框以选择它,我是只是输入这个,因为你是新来的,没有冒犯。
代码:
- 我已经实现了四个类,其中三个是
DTO的
[beans]
:Computer.java
,Load.java
,Location.java
和测试类
:JSONParse.java
。请查看代码!
Computer.java
package com.example.dto;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
公共类计算机{
private String hostname;
私有字符串供应商;
private String model;
private int swap;
private int memory;
@JsonProperty(sess_count)
private int sessCount;
private List< Load>加载;
@JsonProperty(in_use)
private boolean inUse;
@JsonProperty(free_in_tmp)
private int freeInTmp;
@JsonProperty(class)
private String clazz;
private List< Location>地点;
@JsonProperty(last_access)
private String lastAccess;
@JsonProperty(last_report)
private int lastReport;
private String ip;
public String getHostname(){
return hostname;
}
public void setHostname(String hostname){
this.hostname = hostname;
}
public String getVendor(){
return vendor;
}
public void setVendor(String vendor){
this.vendor = vendor;
}
public String getModel(){
return model;
}
public void setModel(String model){
this.model = model;
}
public int getSwap(){
return swap;
}
public void setSwap(int swap){
this.swap = swap;
}
public int getMemory(){
return memory;
}
public void setMemory(int memory){
this.memory = memory;
}
public int getSessCount(){
return sessCount;
}
public void setSessCount(int sessCount){
this.sessCount = sessCount;
}
public List< Load> getLoad(){
返回加载;
}
public void setLoad(List< Load> load){
this.load = load;
}
public boolean isInUse(){
return inUse;
}
public void setInUse(boolean inUse){
this.inUse = inUse;
}
public int getFreeInTmp(){
return freeInTmp;
}
public void setFreeInTmp(int freeInTmp){
this.freeInTmp = freeInTmp;
}
public String getClazz(){
return clazz;
}
public void setClazz(String clazz){
this.clazz = clazz;
}
公开列表<位置> getLocation(){
返回位置;
}
public void setLocation(List< Location> location){
this.location = location;
}
public String getLastAccess(){
return lastAccess;
}
public void setLastAccess(String lastAccess){
this.lastAccess = lastAccess;
}
public int getLastReport(){
return lastReport;
}
public void setLastReport(int lastReport){
this.lastReport = lastReport;
}
public String getIp(){
return ip;
}
public void setIp(String ip){
this.ip = ip;
}
@Override
public String toString(){
String str ================== ================ \r\\\
;
str + =主机名:+主机名+\\ n+ +
供应商:+供应商+\\\\ n+
型号: + model +\\\\ n+
交换:+ swap +\\\\ n+
内存:+内存+\ r \ n+
Sess_Count:+ sessCount +\\\\ n+
加载:+\\\\ n;
for(Load ld:load){
str + =\t+One:+ ld.getOne()+\\\\ n;
str + =\t+Five:+ ld.getFive()+\\\\ n;
str + =\t+十五:+ ld.getFifteen()+\\\\ n;
}
str + =In_Use:+ inUse +\\\\ n+
Free_In_Tmp:+ freeInTmp +\\\\ n +
类:+ clazz +\\ n+
位置:+\\ n;
for(Location lc:location){
str + =\t+Building:+ lc.getBuilding()+\\\\ n;
str + =\t+Computers:+ lc.getComputers()+\\\\ n;
str + =\t+房间:+ lc.getRoom()+\\\\ n;
str + =\t+String:+ lc.getStr()+\\\\ n;
}
str + =Ip:+ ip +\\\\ n;
str + =Last_Access:+ lastAccess +\\\\ n;
str + =Last_Report:+ lastReport +\\\\ n;
return str;
}
}
Load.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
公共类加载{
private int one;
private int five;
private int 15;
public int getOne(){
return one;
}
public void setOne(int one){
this.one = one;
}
public int getFive(){
return five;
}
public void setFive(int five){
this.five = five;
}
public int getFifteen(){
return 15;
}
public void setFifteen(int 15){
this.fifteen = 15;
}
}
Location.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
公共类位置{
private String room;
私人字符串建设;
私人字符串计算机;
@JsonProperty(string)
private String str;
public String getRoom(){
return room;
}
public void setRoom(String room){
this.room = room;
}
public String getBuilding(){
return building;
}
public void setBuilding(String building){
this.building = building;
}
public String getComputers(){
return computers;
}
public void setComputers(String computers){
this.computers = computers;
}
public String getStr(){
return str;
}
public void setStr(String str){
this.str = str;
}
}
JSONParse.java: runnable class
仅用于测试目的。
package com .example.main;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.example.dto.Computer;
公共类JSONParse {
public static void main(String args [])抛出JsonParseException,JsonMappingException,IOException {
/ * JSON provider * /
URL url =新网址(http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505);
ObjectMapper mapper = new ObjectMapper();
/ *
*这允许ObjectMapper接受集合的单个值。
*例如:返回的JSON中的location属性是
*可以接受多个对象的集合,但是在反序列化过程中,这个属性只是
*有一个对象并导致异常。
* /
mapper.configure(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,true);
/ *
*如果某些JSON属性不存在,请避免异常将
* FAIL_ON_UNKNOWN_PROPERTIES设置为false
* /
mapper.configure(功能。 FAIL_ON_UNKNOWN_PROPERTIES,false);
/ *获取所有电脑* /
列表<电脑> computers = mapper.readValue(url,new TypeReference< List< Computer>>(){});
/ *打印每台计算机(以前覆盖toString()方法)* /
(计算机:计算机){
System.out.println(computer.toString) ());
}
}
}
上述类的输出
:
=================================
主机名:pierb505p13。 engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9608460
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.23
Last_Access:
Last_Report:1383202078
=================================
主机名:pierb505p22.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
One:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9609244
等级:L
地点:$ b $ b建筑:PIERPONT
计算:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.32
Last_Access:
Last_Report:1383201987
== ===============================
主机名:pierb505p06.engin.umich.edu
供应商: Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9606464
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.16
Last_Access:
Last_Report:1383201982
======= ==========================
主机名:pierb505p17.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9606820
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.27
Last_Access:
Last_Report:1383202064
================ =================
主机名:pierb505p15.engin.umich.edu
供应商:Linux
型号:Dell 980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9609052
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
每日:141.213.54.25
Last_Access:
Last_Report:1383202052
===================== ============
主机名:pierb505p12.engin.umich.edu
供应商:Linux
型号:Dell 980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp :9607136
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.22
Last_Access:
Last_Report:1383202037
========================== =======
主机名:pierb505p03.engin.umich.edu
供应商:Linux
型号:Dell 980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9603132
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.13
Last_Access:
Last_Report:1383201964
=============================== ==
主机名:pierb505p14.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count: 0
加载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9599592
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.24
Last_Access:
Last_Report:1383201996
=================================
主机名:pierb505p07.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:0
五:0
十五:0
In_Use:false
Free_In_Tmp:9608176
等级:L
位置:
建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.17
Last_Access:
Last_Report: 1383202127
=================================
主机名:pierb505p23。 engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
一:1
五:0
十五:0
In_Use:false
Free_In_Tmp:9600968
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.33
Last_Access:
Last_Report:1383202013
=================================
主机名:pierb505p11.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
One:1
五:0
十五:0
In_Use:false
Free_In_Tmp:9607224
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.21
Last_Access:
Last_Report:1383202098
= ================================
主机名:pierb505p21.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
负载:
一:1
五: 0
十五:0
In_Use:false
Free_In_Tmp:9595136
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.31
Last_Access:
Last_Report:1383202035
====== ======================= ====
主机名:pierb505p16.engin.umich.edu
供应商:Linux
型号:Dell 980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:1
五:0
十五:0
In_Use:false
Free_In_Tmp:9601212
分类: L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.26
Last_Access :
Last_Report:1383202106
=================================
主机名:pierb505p04.engin.umich.edu
供应商:Linux
型号:戴尔980
交换:8126456
内存:7797
Sess_Count:0
加载:
一:1
五:0
十五:0
In_Use:false
Free_In_Tmp:9603220
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.14
Last_Access:
Last_Report:1383202135
=================================
主机名: pierb505p08.en gin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
负载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:16024588
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.18
Last_Access:
Last_Report:1383202133
=================================
主机名:pierb505p25.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
负载:
One:1
五:5
十五:15
In_Use:false
Free_In_Tmp:10299356
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.35
Last_Access:
Last_Report:1383202100
= ================================
主机名:pierb505p26.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
负载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:11303888
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.36
Last_Access:
Last_Report:1383202027
========= ========================
主机名:pierb505p19.engin.umich.edu
供应商:Windows
型号: OptiPlex 980
交换:0
内存:0
Sess_Count:0
负载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:15914176
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.29
Last_Access:
Last_Report:1383202081
============== ===================
主机名:pierb505p09.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
加载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:29956064
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.19
Last_Access:
Last_Report:1383201997
======================= ==========
主机名:pierb505p10.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
加载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:19663960
等级:L
地点:$ b $ b建筑:PIERPONT
计算机:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213。 54.20
Last_Access:
Last_Report:1383202003
============================ =====
主机名:pierb505p24.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
加载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:19197988
等级:L
地点:$ b $ b建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.34
Last_Access:
Last_Report:1383202038
=================================
主机名:pierb505p20.engin.umich.edu
供应商:Windows
型号:OptiPlex 980
交换:0
内存:0
Sess_Count:0
负载:
一:1
五:5
十五:15
In_Use:false
Free_In_Tmp:9585536
等级:L
位置:
建筑:PIERPONT
电脑:26
房间:B505
字符串:B505 PIERPONT
Ip:141.213.54.30
Last_Access:
Last_Report:1383201994
享受!
I'm a spring newbie. Wrote really simple code to get an array of JSON object from the API call at http://api.engin.umich.edu/hostinfo/...PONT&room=B505
Only get "Name: NULL"
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Computer[] computer = restTemplate.getForObject("http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505", Computer[].class);
System.out.println("Name: " + computer[0].getName());
}
}
Here goes the simple computer class.
package hello;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Computer {
private String hostname;
public String getName() {
return hostname;
}
public String toString() {
return "Computer [hostname=" + hostname + "]";
}
}
Tested and working!
Some observations:
If you don't know so much about
Spring
framework
, it doesn't matter. Toserialize/deserialize
JSON
can be done in many ways.I have looked that your
JSON
contains somereserved
words likeclass
orstring
. In order to handle this you will need to use@JsonProperty
annotation (you will see it later atComputer.java
and others). I also used this annotation for not declaring somevariables
that in yourJSON
properties contain underscores (remember that yourDTO's
[beans]
must have the samestructure
,datatypes
and also use the sameJSON
property names).To resolve the problem regarding your
RestTemplate
object
(I am not sure if you are doing it the right way when usingComputer[].class
). As a recommendation, please consider following/checking thiscode
snippet: https://stackoverflow.com/a/6349488/1178686
- This is just personal: If you consider this answer fits you requirement, don't forget to click the check next to the answer to select it, I am just typing this because you are new here, no offense.
Code:
- I have implemented four classes, three of them as
DTO's
[beans]
:Computer.java
,Load.java
,Location.java
and a testclass
:JSONParse.java
. Please review the code!
Computer.java
package com.example.dto;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class Computer {
private String hostname;
private String vendor;
private String model;
private int swap;
private int memory;
@JsonProperty("sess_count")
private int sessCount;
private List<Load> load;
@JsonProperty("in_use")
private boolean inUse;
@JsonProperty("free_in_tmp")
private int freeInTmp;
@JsonProperty("class")
private String clazz;
private List<Location> location;
@JsonProperty("last_access")
private String lastAccess;
@JsonProperty("last_report")
private int lastReport;
private String ip;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSwap() {
return swap;
}
public void setSwap(int swap) {
this.swap = swap;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
public int getSessCount() {
return sessCount;
}
public void setSessCount(int sessCount) {
this.sessCount = sessCount;
}
public List<Load> getLoad() {
return load;
}
public void setLoad(List<Load> load) {
this.load = load;
}
public boolean isInUse() {
return inUse;
}
public void setInUse(boolean inUse) {
this.inUse = inUse;
}
public int getFreeInTmp() {
return freeInTmp;
}
public void setFreeInTmp(int freeInTmp) {
this.freeInTmp = freeInTmp;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public List<Location> getLocation() {
return location;
}
public void setLocation(List<Location> location) {
this.location = location;
}
public String getLastAccess() {
return lastAccess;
}
public void setLastAccess(String lastAccess) {
this.lastAccess = lastAccess;
}
public int getLastReport() {
return lastReport;
}
public void setLastReport(int lastReport) {
this.lastReport = lastReport;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
@Override
public String toString() {
String str = "=================================\r\n";
str += "Hostname: " + hostname + "\r\n" +
"Vendor: " + vendor + "\r\n" +
"Model: " + model + "\r\n" +
"Swap: " + swap + "\r\n" +
"Memory: " + memory + "\r\n" +
"Sess_Count: " + sessCount + "\r\n" +
"Load: " + "\r\n";
for(Load ld : load) {
str += "\t" + "One: " + ld.getOne() + "\r\n";
str += "\t" + "Five: " + ld.getFive() + "\r\n";
str += "\t" + "Fifteen: " + ld.getFifteen() + "\r\n";
}
str += "In_Use: " + inUse + "\r\n" +
"Free_In_Tmp: " + freeInTmp + "\r\n" +
"Class: " + clazz + "\r\n" +
"Location: " + "\r\n";
for(Location lc : location) {
str += "\t" + "Building: " + lc.getBuilding() + "\r\n";
str += "\t" + "Computers: " + lc.getComputers() + "\r\n";
str += "\t" + "Room: " + lc.getRoom() + "\r\n";
str += "\t" + "String: " + lc.getStr() + "\r\n";
}
str += "Ip: " + ip + "\r\n";
str += "Last_Access: " + lastAccess + "\r\n";
str += "Last_Report: " + lastReport + "\r\n";
return str;
}
}
Load.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Load {
private int one;
private int five;
private int fifteen;
public int getOne() {
return one;
}
public void setOne(int one) {
this.one = one;
}
public int getFive() {
return five;
}
public void setFive(int five) {
this.five = five;
}
public int getFifteen() {
return fifteen;
}
public void setFifteen(int fifteen) {
this.fifteen = fifteen;
}
}
Location.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class Location {
private String room;
private String building;
private String computers;
@JsonProperty("string")
private String str;
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getBuilding() {
return building;
}
public void setBuilding(String building) {
this.building = building;
}
public String getComputers() {
return computers;
}
public void setComputers(String computers) {
this.computers = computers;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
JSONParse.java: runnable class
just for testing purposes.
package com.example.main;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.example.dto.Computer;
public class JSONParse {
public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
/* JSON provider */
URL url = new URL("http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505");
ObjectMapper mapper = new ObjectMapper();
/*
* This allows the ObjectMapper to accept single values for a collection.
* For example: "location" property in the returned JSON is a collection that
* can accept multiple objects but, in deserialization process, this property just
* have one object and causes an Exception.
*/
mapper.configure(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
/*
* If some JSON property is not present, avoid exceptions setting
* FAIL_ON_UNKNOWN_PROPERTIES to false
*/
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
/* Get all computers */
List<Computer> computers = mapper.readValue(url, new TypeReference<List<Computer>>(){});
/* Print each computer (previously overriding "toString()" method) */
for(Computer computer : computers) {
System.out.println(computer.toString());
}
}
}
Output from the above class
:
================================= Hostname: pierb505p13.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9608460 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.23 Last_Access: Last_Report: 1383202078 ================================= Hostname: pierb505p22.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9609244 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.32 Last_Access: Last_Report: 1383201987 ================================= Hostname: pierb505p06.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9606464 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.16 Last_Access: Last_Report: 1383201982 ================================= Hostname: pierb505p17.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9606820 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.27 Last_Access: Last_Report: 1383202064 ================================= Hostname: pierb505p15.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9609052 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.25 Last_Access: Last_Report: 1383202052 ================================= Hostname: pierb505p12.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9607136 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.22 Last_Access: Last_Report: 1383202037 ================================= Hostname: pierb505p03.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9603132 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.13 Last_Access: Last_Report: 1383201964 ================================= Hostname: pierb505p14.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9599592 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.24 Last_Access: Last_Report: 1383201996 ================================= Hostname: pierb505p07.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 0 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9608176 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.17 Last_Access: Last_Report: 1383202127 ================================= Hostname: pierb505p23.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 1 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9600968 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.33 Last_Access: Last_Report: 1383202013 ================================= Hostname: pierb505p11.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 1 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9607224 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.21 Last_Access: Last_Report: 1383202098 ================================= Hostname: pierb505p21.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 1 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9595136 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.31 Last_Access: Last_Report: 1383202035 ================================= Hostname: pierb505p16.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 1 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9601212 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.26 Last_Access: Last_Report: 1383202106 ================================= Hostname: pierb505p04.engin.umich.edu Vendor: Linux Model: Dell 980 Swap: 8126456 Memory: 7797 Sess_Count: 0 Load: One: 1 Five: 0 Fifteen: 0 In_Use: false Free_In_Tmp: 9603220 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.14 Last_Access: Last_Report: 1383202135 ================================= Hostname: pierb505p08.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 16024588 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.18 Last_Access: Last_Report: 1383202133 ================================= Hostname: pierb505p25.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 10299356 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.35 Last_Access: Last_Report: 1383202100 ================================= Hostname: pierb505p26.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 11303888 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.36 Last_Access: Last_Report: 1383202027 ================================= Hostname: pierb505p19.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 15914176 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.29 Last_Access: Last_Report: 1383202081 ================================= Hostname: pierb505p09.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 29956064 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.19 Last_Access: Last_Report: 1383201997 ================================= Hostname: pierb505p10.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 19663960 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.20 Last_Access: Last_Report: 1383202003 ================================= Hostname: pierb505p24.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 19197988 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.34 Last_Access: Last_Report: 1383202038 ================================= Hostname: pierb505p20.engin.umich.edu Vendor: Windows Model: OptiPlex 980 Swap: 0 Memory: 0 Sess_Count: 0 Load: One: 1 Five: 5 Fifteen: 15 In_Use: false Free_In_Tmp: 9585536 Class: L Location: Building: PIERPONT Computers: 26 Room: B505 String: B505 PIERPONT Ip: 141.213.54.30 Last_Access: Last_Report: 1383201994
Enjoy!
这篇关于用于解析JSON的简单Spring代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!