Java 处理 JSON

6 篇文章 0 订阅
Java 处理 JSON

Java 处理 JSON 需要的第三方库:
 以上包的合集下载地址:http://download.csdn.net/download/gaidandan/7335445

对于与 JSONObject 进行转化的 Java 对象,都要将其实现为一个 JavaBean;
关于 JavaBean 最简单的规定如下:
  • 提供一个默认的无参构造函数;
  • 需要被序列化并且实现了Serializable接口;
  • 可能有一系列可读写属性;
  • 可能有一系列的"getter"或"setter"方法;

以下是示例用的JavaBean
1
public class User {
2
    private String first = null;
3
    private String lastName = null;
4
    public User() {
5
    }
6
    public User(String first, String lastName) {
7
        this.first = first;
8
        this.lastName = lastName;
9
    }
10
    public String getFirst() {
11
        return first;
12
    }
13
    public void setFirst(String first) {
14
        this.first = first;
15
    }
16
    public String getLastName() {
17
        return lastName;
18
    }
19
    public void setLastName(String lastName) {
20
        this.lastName = lastName;
21
    }
22
}

将 Java 对象转化为 JSON 字符串
要将 Java 对象转化的 JSON 格式的字符串,要先确保该 Java 对象已经实现为一个JavaBean,首先将 Java 对象转化为 JSONObject对象,再转化为字符串;
1
User user = new User("Alssad","Alex");
2
3
JSONObject jsonObj = JSONObject.fromObject(user);
4
String jsonStr = jsonObj.toString();
5
6
//jsonStr内容: 
7
{"lastName":"Alex","first":"Alssad"}

将 Java 对象列表转化为 JSONArray,并转化为字符串
要将 Java 对象列表转化的 JSON 格式的字符串,首先将 Java 对象列表转化为 JSONArray对象,再转化为字符串;
1
List<User> list = new ArrayList<User>();
2
list.add(new User("John","Doe"));
3
list.add(new User("Anna","Smith"));
4
list.add(new User("Peter","Jones"));
5
6
JSONArray jsonArr = JSONArray.fromObject(list);
7
String jsonStr = jsonArr.toString();
8
9
//jsonStr内容:   
10
[{"lastName":"Doe","first":"John"},{"lastName":"Smith","first":"Anna"},{"lastName":"Jones","first":"Peter"}]


将 JSON 字符串转化为 Java 对象
1
//{"lastName":"Alex","first":"Alssad"}
2
String jsonStr =  "{\"lastName\":\"Alex\",\"first\":\"Alssad\"}";
3
4
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
5
User user = (User)JSONObject.toBean(jsonObj,User.class);

将 JSON 字符串转化为 Java 对象数组
1
//[{"lastName":"Doe","first":"John"},{"lastName":"Smith","first":"Anna"},{"lastName":"Jones","first":"Peter"}]
2
String jsonStr =  "[{\"lastName\":\"Doe\",\"first\":\"John\"},{\"lastName\":\"Smith\",\"first\":\"Anna\"},{\"lastName\":\"Jones\",\"first\":\"Peter\"}]";
3
4
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
5
List<User> list = (List<User>)JSONArray.toList(jsonArray,User.class);




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
处理嵌套json格式的数据。。。 public static void main(String[] args) { // 官方API http://www.json.org/java/ /* 购物车中信息 goods_cart={cart_1325036696007:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"b555bfj05d7dcg307h91323398584156",specsstr:"颜色:黑色 尺寸:L",price:4765,stock:15,count:6},cart_1325036702105:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"787a9f5he93chcifh951323398314484",specsstr:"颜色:黑色 尺寸:XL",price:4700.15,stock:12,count:1},cart_1325136643984:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg",specs:"8466347bi6eia43hd6j1323398639859",specsstr:"颜色:灰色 尺寸:XL",price:4600,stock:3,count:1}}; * **/ try{ String s0 = "{cart_1325036696007:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"b555bfj05d7dcg307h91323398584156",specsstr:"颜色:黑色 尺寸:L",price:4765,stock:15,count:6},cart_1325036702105:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"787a9f5he93chcifh951323398314484",specsstr:"颜色:黑色 尺寸:XL",price:4700.15,stock:12,count:1},cart_1325136643984:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg",specs:"8466347bi6eia43hd6j1323398639859",specsstr:"颜色:灰色 尺寸:XL",price:4600,stock:3,count:1}};"; String s= java.net.URLDecoder.decode(s0, "utf-8"); System.out.println(s); JSONObject o = new JSONObject(s); System.out.println(o.get("cart_1325036696007")); //根据属性,获取值 System.out.println(o.toString()); //得到字符串 System.out.println(o.names().get(2)); //获取对象中第三组属性名 System.out.println(o.names().length()); //获取对象中属性个数 //System.out.println(o.names().getJSONArray(1)); //获取对象中属性个数 //names(jsonObjectName) 私有方法 获取该对象的所有属性名,返回成JSONArray。 //JSONObject.getNames(jsonObjectName) 静态方法 获取对象的所有属性名,返回成数组。 System.out.println(JSONObject.getNames(o.getJSONObject("cart_1325036696007"))[1]); System.out.println(o.getJSONObject("cart_1325036696007").names().get(1)); System.out.println(o.length()); //共有几组对象 System.out.println(o.has("cart_1325036696007")); //有无该该值 /* 遍历json中的每一组元素*/ String name = null; JSONObject t_o = null; for(int i=0; i<o.length(); i++){ name = JSONObject.getNames(o)[i]; System.out.println("商品项ID:"+name); t_o = o.getJSONObject(name); for(int j=0; j< t_o.length(); j++){ name = JSONObject.getNames(t_o)[j]; System.out.print(name+":"+t_o.get(name)+" "); } System.out.println(); } }catch(Exception e){ e.printStackTrace(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值