将数据转换为json格式代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baoxiaofeicsdn/article/details/49906147

本文数据转json格式的入门代码,创建java文件即可

用于将实体信息封装成json格式数据
使用工具:myeclipse
导入包gson.jar
项目结构:


SchoolInfo.java
public class SchoolInfo {
	private String mSchoolName;
	
	public void setSchoolName(String schoolName){
		this.mSchoolName = schoolName;
	}
	
	public String getSchoolName(){
		return mSchoolName;		
	}
}
Person.java
import java.util.List;

public class Person {
	private String mName;
	private int mAge;
	private String mUrl;
	private List<SchoolInfo> mSchoolInfo;
	
	/*get方法*/
	public String getName(){
		return mName;
	}
	public int getAge(){
		return mAge;
	}
	public String getUrl(){
		return mUrl;
	}
	public List<SchoolInfo> getSchoolInfo(){
		return mSchoolInfo;
	}
	
	/*set方法*/
	public void setName(String name){
		this.mName = name;
	}
	public void setAge(int age){
		this.mAge = age;
	}
	public void setUrl(String url){
		this.mUrl = url;
	}
	public void setSchoolInfo(List<SchoolInfo> schoolInfo){
		this.mSchoolInfo = schoolInfo;
	}	
}
Result.java
import java.util.List;


public class Result {
	private int mResult;
	private List<Person> mPersonData;
	
	/*get方法*/
	public int getResult(){
		return mResult;
	}
	public List<Person> getPersonData(){
		return mPersonData;
	}
	
	/*set方法*/
	public void setResult(int result){
		this.mResult = result;
	}
	public void setPersonData(List<Person> personData){
		this.mPersonData = personData;
	}
	
}
TestJson.java
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
/**
 * 生成json代码,封装实体数据
 * @author xiaoyuer
 *
 */
public class TestJson {
	
	/*封装的概念,一级管理一级的封装,不要跨级封装*/
	public static void main(String[] args){	
		Result result = new Result();
		result.setResult(1);
		
		List<Person> personData = new ArrayList<Person>();
		result.setPersonData(personData);
		
		Person person1 = new Person();
		person1.setName("nate");
		person1.setAge(12);
		person1.setUrl("http://h.hiphotos.baidu.com/image/pic/item/6c224f4a20a446239e8d311c9b22720e0cf3d70d.jpg");
		
		List<SchoolInfo> schoolInfos = new ArrayList<SchoolInfo>();
		SchoolInfo schoolInfo1 = new SchoolInfo();
		SchoolInfo schoolInfo2 = new SchoolInfo();
		schoolInfo1.setSchoolName("北大");
		schoolInfo2.setSchoolName("清华");			
		schoolInfos.add(schoolInfo1);
		schoolInfos.add(schoolInfo2);
		
		person1.setSchoolInfo(schoolInfos);
		personData.add(person1);	
		Gson gson = new Gson();
		
		//打印,通过toJson方法将所有的数据转化为json格式
		System.out.println(gson.toJson(result));
		
		
	}
	
}
阅读更多

java 将数据转换json格式

12-05

SET FOREIGN_KEY_CHECKS=0;rn-- ----------------------------rn-- Table structure for `dictionary`rn-- ----------------------------rnDROP TABLE IF EXISTS `dictionary`;rnCREATE TABLE `dictionary` (rn `id` int(11) NOT NULL AUTO_INCREMENT,rn `topic_id` int(11) DEFAULT NULL COMMENT '主标题ID',rn `item_name` varchar(100) NOT NULL COMMENT '值',rn `parent_id` int(11) DEFAULT NULL,rn `description` varchar(100) DEFAULT NULL COMMENT '说明',rn `the_sort` int(11) DEFAULT '1000' COMMENT '排序',rn PRIMARY KEY (`id`),rn KEY `ix_category_id` (`topic_id`) USING BTREErn) ENGINE=InnoDB AUTO_INCREMENT=586 DEFAULT CHARSET=utf8 COMMENT='数据字典项';rnrn-- ----------------------------rn-- Records of `dictionary`rn-- ----------------------------rnINSERT INTO `dictionary` VALUES ('303', '4', '医学', null, null, '1000');rnINSERT INTO `dictionary` VALUES ('304', '4', '外科', '303', null, '1000');rnINSERT INTO `dictionary` VALUES ('305', '4', '内科', '303', null, '1000');rnINSERT INTO `dictionary` VALUES ('314', '4', '中医类', '303', null, '1000');rnINSERT INTO `dictionary` VALUES ('315', '4', '药剂类', '303', null, '1000');rnINSERT INTO `dictionary` VALUES ('316', '4', '普外科', '304', null, '1000');rnINSERT INTO `dictionary` VALUES ('319', '4', '神经外科', '304', null, '1000');rnINSERT INTO `dictionary` VALUES ('325', '4', '消化内科', '305', null, '1000');rnINSERT INTO `dictionary` VALUES ('371', '4', '中医医疗', '314', null, '1000');rnINSERT INTO `dictionary` VALUES ('373', '4', '中药', '315', null, '1000');rnrnrnrnjava如何将上面的数据 转换成json格式,用于tree生成,parent_id 是上下级关联IDrnrn结果如下:rn[rn text:医学,children:[rn text:外科,children:[rn text:普外科,leaf:true,rn text:神经外科,leaf:truern ],rn text:内科,children:[rn text:消化内科,leaf:truern ],rn ........内容同上rn ]rn]rnrn求实现的例子,java程序 or 其它rnrn

没有更多推荐了,返回首页