关于JSON详解编程语言

Java基本类型,List,Map 转化为JSON格式:

Person.java

import java.util.Date; 
 
 
public class Person { 
	private String name; 
	private int age; 
	private String birthday; 
	public Person(String name,int age,String birthday){ 
		this.name = name; 
		this.age = age; 
		this.birthday = birthday; 
	} 
	public String getName() { 
		return name; 
	} 
	public void setName(String name) { 
		this.name = name; 
	} 
	public int getAge() { 
		return age; 
	} 
	public void setAge(int age) { 
		this.age = age; 
	} 
	public String getBirthday() { 
		return birthday; 
	} 
	public void setBirthday(String birthday) { 
		this.birthday = birthday; 
	} 
	 
} 

JSONTest.java

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.zip.DataFormatException; 
 
import net.sf.json.JSONArray; 
 
public class JSONTest { 
	private String title; // 标题 
	private String author; // 作者 
	private float price; // 定价 
	private String publication; // 出版日期 
 
	private List<Person> persons = new ArrayList<Person>(); // List 
	private Map map = new HashMap(); // Map 
 
	public String getTitle() { 
		return title; 
	} 
 
	public void setTitle(String title) { 
		this.title = title; 
	} 
 
	public String getAuthor() { 
		return author; 
	} 
 
	public void setAuthor(String author) { 
		this.author = author; 
	} 
 
	public float getPrice() { 
		return price; 
	} 
 
	public void setPrice(float price) { 
		this.price = price; 
	} 
 
	public String getPublication() { 
		return publication; 
	} 
 
	public void setPublication(String publication) { 
		this.publication = publication; 
	} 
 
	public List<Person> getPersons() { 
		return persons; 
	} 
 
	public void setPersons(List<Person> persons) { 
		this.persons = persons; 
	} 
 
	public Map getMap() { 
		return map; 
	} 
 
	public void setMap(Map map) { 
		this.map = map; 
	} 
 
	public static void main(String[] args) throws ParseException { 
		JSONTest mj = new JSONTest(); 
 
		// 日期设置 
		Date d = new Date(); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		String date = sdf.format(d); 
 
		// 为属性设置值 
		mj.setTitle("乔布斯传记"); 
		mj.setAuthor("未知"); 
		mj.setPrice(100.0f); 
	    mj.setPublication(date); 
 
		// 设置List 
		List<Person> list = new ArrayList<Person>(); 
		list.add(new Person("zhangsan", 20, date)); 
		mj.setPersons(list); 
 
		// 设置Map 
		Map map = new HashMap(); 
		map.put("a", "i am a"); 
		map.put("b", "i am b"); 
		mj.setMap(map); 
 
		//调用JSONArray.fromObject()方法 转化为JSON对象 
		JSONArray jsons = JSONArray.fromObject(mj); 
		System.out.println(jsons.toString()); 
	} 
} 

在使用JSON时 不只是引入一个JSON包就可以的 不然会报java.lang.ClassNotFoundException:异常

JSON所依赖jar包如下(共6个)

commons-beanutils-1.8.3.jar

commons-lang-2.1.jar

ezmorph-1.0.2.jar

json-lib-2.4-jdk15.jar

如果出现如下错误 说明还缺少如下包

java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap

commons-collections-3.2.jar

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

commons-logging-1.1.1.jar

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/14268.html

(0)
上一篇 2021年7月19日 16:44
下一篇 2021年7月19日 16:44

相关推荐

发表回复

登录后才能评论