http://www.verejava.com/?id=16998617072749
下载依赖 jar 包
json.jar
package com.json9;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public class Test
{
public static void main(String[] args)
{
/*
Map<String,String> map=new HashMap<String,String>();
Map 对象存入 用户名,密码,电话号码
*/
Map<String,String> map=new HashMap<String,String>();
//Map 对象存入 用户名,密码,电话号码
map.put("username", "liyang");
map.put("pwd", "111111");
map.put("telephone", "152232323");
//Map 转成 JSONObject 字符串
JSONObject jsonObj=new JSONObject(map);
System.out.println(jsonObj.toString());
}
}
package com.json10;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public class Test
{
public static void main(String[] args)
{
/*
Map<String,Object> map=new HashMap<String,Object>();
Map 对象存入 用户名,密码,年龄
*/
Map<String,Object> map=new HashMap<String,Object>();
//Map 对象存入 用户名,密码,年龄
map.put("username", "admin");
map.put("pwd", "22");
map.put("age", 20);
JSONObject jsonObj=new JSONObject(map);
System.out.println(jsonObj.toString());
}
}
package com.json11;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.json7.House;
public class Test
{
public static void main(String[] args)
{
/*
Map<String,Object> map=new HashMap<String,Object>();
Map 对象存入 姓名,年龄, House 对象
*/
Map<String,Object> map=new HashMap<String,Object>();
//Map 对象存入 姓名,年龄, House 对象
map.put("name", "李俊");
map.put("age", 30);
map.put("house", new House(1,"理会","3室2厅",5500,false));
JSONObject jsonObj=new JSONObject(map);
System.out.println(jsonObj.toString());
}
}
package com.json7;
public class House
{
//编号, 房东,房屋描述,房屋价格,是否出租
private int id;
private String owner;
private String description;
private double price;
private boolean isRent;
public House(int id, String owner, String description, double price,
boolean isRent)
{
super();
this.id = id;
this.owner = owner;
this.description = description;
this.price = price;
this.isRent = isRent;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getOwner()
{
return owner;
}
public void setOwner(String owner)
{
this.owner = owner;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public boolean isRent()
{
return isRent;
}
public void setRent(boolean isRent)
{
this.isRent = isRent;
}
}
package com.json12;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.json7.House;
public class Test
{
public static void main(String[] args)
{
/*
Map<String,Object> map=new HashMap<String,Object>();
Map 对象存入 姓名,年龄, House 对象,颜色数组
*/
Map<String,Object> map=new HashMap<String,Object>();
//Map 对象存入 姓名,年龄, House 对象,颜色数组
map.put("name", "王涛");
map.put("age", 40);
map.put("house", new House(2,"等均","1室1厅",3000,false));
map.put("color", new String[]{"红色","蓝色","绿色"});
JSONObject jsonObj=new JSONObject(map);
System.out.println(jsonObj.toString());
}
}