JSON字符串转成实体类

loverGTO 2018-10-26 11:43:00
JSON字符串转成实体类

class A{
int count;
List<B> infos;
}
class B{
int id;
String name;
}

根据json字符串

String jsonStr = "{\"unuse\":\"A中没有的字段\",\"count\":\"2\",\"infos\":[{\"withOut\":\"B中没有的字段\",\"id\":\"119\",\"name\":\"zhansan\"},{\"withOut\":\"B中没有的字段\",\"id\":\"9527\",\"name\":\"lisi\"}]}";

如何得到一个A对象,并包含字符串中对应的字段值
...全文
514 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
游一游走一走 2018-10-26
  • 打赏
  • 举报
回复
希望可以帮到你

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 + '\'' +
                    '}';
        }
    }
}

loverGTO 2018-10-26
  • 打赏
  • 举报
回复
类A

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
张家辉
古天乐
loverGTO 2018-10-26
  • 打赏
  • 举报
回复
引用 5 楼 qq_40224672 的回复:

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;
	}
}

可以的朋友楼下我贴一下我的源码把,之前想太复杂了,想到用反射去给类A的属性赋值,然后到infos的时候又想到去判断类A属性的类型,判断类型是否为基础类型,对于不是基础类型的再递归给类B的属性赋值,结果发现光判断是否为基础类就一大堆,
枫舞蝶刺 2018-10-26
  • 打赏
  • 举报
回复

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;
}
}

十八道胡同 2018-10-26
  • 打赏
  • 举报
回复
引用 2 楼 zyz1985 的回复:
希望可以帮到你

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 + '\'' +
'}';
}
}
}



loverGTO 2018-10-26
  • 打赏
  • 举报
回复
引用 2 楼 zyz1985 的回复:
希望可以帮到你

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 + '\'' +
                    '}';
        }
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)//注解可以理解 static class B//如果我的class全是public的呢-------->public static class ? B对应的是一个数据库实体表,成千上万个B实例化到内存中等待调用 不知道我对static的理解对不对,这样问是否正确,还望指出, 而且这两者还缺一不可,

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧