80,351
社区成员
发帖
与我相关
我的任务
分享
package com.devil.pojo;
import java.util.List;
public class A {
private List<B> array;
private int size;
public List<B> getArray() {
return array;
}
public void setArray(List<B> array) {
this.array = array;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
package com.devil.pojo;
public class B {
private String UID;
private String IP;
public String getUID() {
return UID;
}
public void setUID(String uID) {
UID = uID;
}
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
}
private void T() {
String json = "{'array':[{'UID':'LACA6C0284SGCM8','IP':'192.168.1.112'},{'UID':'LACA6C0284UMFKY','IP':'192.168.1.127'},{'UID':'LACA6C0284KCXNU','IP':'192.168.1.106'}],'size':3}";
A a = JsonUtil.parse(json, A.class);
for (B b : a.getArray()) {
Log.e("DEVIL", b.getUID() + " " + b.getIP());
}
}
03-20 14:00:53.869: E/DEVIL(12003): LACA6C0284SGCM8 192.168.1.112
03-20 14:00:53.869: E/DEVIL(12003): LACA6C0284UMFKY 192.168.1.127
03-20 14:00:53.869: E/DEVIL(12003): LACA6C0284KCXNU 192.168.1.106
/**
* 将json字符串转换成对象
*
* @param json
* @param type
* @return
*/
public static <T> T parse(String json, Class<T> type) {
Gson gson = new Gson();
T t = null;
try {
t = gson.fromJson(json, type);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return t;
}