67,550
社区成员




class A{
int count;
List<B> infos;
}
class B{
int id;
String name;
}
String jsonStr = "{\"unuse\":\"A中没有的字段\",\"count\":\"2\",\"infos\":[{\"withOut\":\"B中没有的字段\",\"id\":\"119\",\"name\":\"zhansan\"},{\"withOut\":\"B中没有的字段\",\"id\":\"9527\",\"name\":\"lisi\"}]}";
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
public class Test {
@org.junit.Test
public void test() throws IOException {
String jsonStr = "{\"unuse\":\"A中没有的字段\",\"count\":\"2\",\"infos\":[{\"withOut\":\"B中没有的字段\",\"id\":\"119\",\"name\":\"zhansan\"},{\"withOut\":\"B中没有的字段\",\"id\":\"9527\",\"name\":\"lisi\"}]}";
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.readValue(jsonStr, A.class));
}
@JsonIgnoreProperties(ignoreUnknown = true)
static class A {
int count;
List<B> infos;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<B> getInfos() {
return infos;
}
public void setInfos(List<B> infos) {
this.infos = infos;
}
@Override
public String toString() {
return "A{" +
"count=" + count +
", infos=" + infos +
'}';
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
static class B {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "B{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
public class QrcustOutput{
int count;
List<CustUser> infos;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<CustUser> getInfos() {
return infos;
}
public void setInfos(List<CustUser> infos) {
this.infos = infos;
}
@Override
public String toString() {
return "QrcustOutput [count=" + count + ", infos=" + infos + "]";
}
}
类B
@Entity
@Table(name = "cust_user")
public class CustUser implements Serializable {
/** @Fields serialVersionUID : TODO(Describe)
*/
private static final long serialVersionUID = -2873447728024852037L;
@Column(name = "corpno", length = 4)
private String corpno;// 法人代码
@Column(name = "custno", length = 16)
private String custno;// 客户号
@Column(name = "custna", length = 100)
private String custna;// 客户名称
@Id
@Column(name = "idtftp", length = 2)
private String idtftp;// 证件类型
@Id
@Column(name = "idtfno", length = 80)
private String idtfno;// 证件号码
@Column(name = "birthd", length = 8)
private String birthd;// 出生日期
@Column(name = "sextyp", length = 1)
private String sextyp;// 性别
@Column(name = "addrcd", length = 50)
private String addrcd;// 所在行政
@Column(name = "teleno", length = 80)
private String teleno;// 电话号码
@Column(name = "emails", length = 200)
private String emails;// 邮箱
@Column(name = "postcd", length = 10)
private String postcd;// 邮编
@Column(name = "addres", length = 255)
private String addres;// 地址
@Column(name = "brchno", length = 10)
private String brchno;// 开户部门
@Column(name = "opendt", length = 8)
private String opendt;// 开户日期
@Column(name = "opensq", length = 32)
private String opensq;// 开户流水
@Column(name = "closdt", length = 8)
private String closdt;// 销户日期
@Column(name = "clossq", length = 32)
private String clossq;// 销户流水
@Column(name = "custst", length = 1)
private String custst;// 客户状态
@Column(name = "datetm", length = 8)
private String datetm;// 维护日期
@Column(name = "timetm", length = 19)
private String timetm;// 时间戳
@Column(name = "cuslvl", length = 2)
private String cuslvl;// 客户级别
@Column(name = "emplcu", length = 1)
private String emplcu;//
}
或者是其他的类C类D...
作为一个通配的接口,不知道传进来的json字符串是什么类的json格式字符串,
json:
{
comm:{
keyword:qrcust
}
input:{
// Data json string
}
}
根据keyword确定Data json string 的类 代码中的className
@Test
public void Test0003() throws ClassNotFoundException, JsonParseException, JsonMappingException, IOException, InstantiationException, IllegalAccessException{
//自己定义了一个data json string 含有不在className类里面的有对应属性的值 ints
String str = "{\"ints\":2,\"count\":2,\"infos\":[{\"addrcd\":\"\",\"addres\":\"中国香港\",\"birthd\":\"\",\"brchno\":\"\",\"closdt\":\"\",\"clossq\":\"\",\"corpno\":\"\",\"cuslvl\":\"\",\"custna\":\"张家辉\",\"custno\":\"7011001100002\",\"custst\":\"1\",\"datetm\":\"\",\"emails\":\"\",\"emplcu\":\"\",\"idtfno\":\"5*****198612030398\",\"idtftp\":\"01\",\"opendt\":\"\",\"opensq\":\"\",\"postcd\":\"\",\"sextyp\":\"1\",\"teleno\":\"185*****359\",\"timetm\":\"\"},{\"addrcd\":\"\",\"addres\":\"中国\",\"birthd\":\"\",\"brchno\":\"\",\"closdt\":\"\",\"clossq\":\"\",\"corpno\":\"\",\"cuslvl\":\"\",\"custna\":\"古天乐\",\"custno\":\"7011001100001\",\"custst\":\"2\",\"datetm\":\"\",\"emails\":\"\",\"emplcu\":\"\",\"idtfno\":\"5*****199112030398\",\"idtftp\":\"01\",\"opendt\":\"\",\"opensq\":\"\",\"postcd\":\"\",\"sextyp\":\"1\",\"teleno\":\"15*****35559\",\"timetm\":\"\"}]}";
String className = "cn.spring.mvn.core.account.zport.QrcustOutput";
Class<?> clazz = Class.forName(className);
Object obj = clazz.newInstance();
obj = new Gson().fromJson(str, clazz);
System.out.println(obj);
QrcustOutput out = (QrcustOutput)obj;
System.out.println(out.getCount());
List<CustUser> infos = out.getInfos();
for (CustUser custUser : infos) {
System.out.println(custUser.getCustna());
}
运行的结果
QrcustOutput [count=2, infos=[CustUser [corpno=, custno=7011001100002, custna=张家辉, idtftp=01, idtfno=5*****198612030398, birthd=, sextyp=1, addrcd=, teleno=185*****359, emails=, postcd=, addres=中国香港, brchno=, opendt=, opensq=, closdt=, clossq=, custst=1, datetm=, timetm=, cuslvl=, emplcu=], CustUser [corpno=, custno=7011001100001, custna=古天乐, idtftp=01, idtfno=5*****199112030398, birthd=, sextyp=1, addrcd=, teleno=15*****35559, emails=, postcd=, addres=中国, brchno=, opendt=, opensq=, closdt=, clossq=, custst=2, datetm=, timetm=, cuslvl=, emplcu=]]]
2
张家辉
古天乐
import java.util.List;
import com.google.gson.Gson;
public class test3 {
public static void main(String[] args) {
String jsonStr = "{\"unuse\":\"A中没有的字段\",\"count\":\"2\",\"infos\":[{\"withOut\":\"B中没有的字段\",\"id\":\"119\",\"name\":\"zhansan\"},{\"withOut\":\"B中没有的字段\",\"id\":\"9527\",\"name\":\"lisi\"}]}";
A a = new Gson().fromJson(jsonStr, A.class);
System.out.println(a.count);
for (int i = 0; i < a.infos.size(); i++) {
System.out.println(a.infos.get(i).id);
System.out.println(a.infos.get(i).name);
}
}
class A{
int count;
List<B> infos;
}
class B{
int id;
String name;
}
}