JSONObject JSONArray

一、JAR包简介

      要使程序可以运行必须引入json-lib包,json-lib包同时依赖于以下的jar包(当时环境下所配备的jar版本):

      1.commons-lang-2.4.jar (注:不是lang3版本的jar包,若是引入commons-lang3-3.1.jar,会发生                    java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException)

      2.commons-beanutils-1.8.3.jar

      3.commons-collections-3.2.1.jar

      4.commons-logging-1.1.jar 

      5.ezmorph-1.0.6.jar

      6.json-lib-2.4-jdk15.jar

      ************import net.sf.json.JSONObject; 而不是org.json.simple.JSONObject ************

 

二、例子(新建Book对象包含属性name和price)

1、从Object到String:先用Object对象构造一个JSONObject对象或是JSONArray对象,再调用toString()方法

示例一:object——>String

package renren.action;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Json {
 public static void main(String[] args) {
  Book book=new Book();
  book.setName("Java");
  book.setPrice(52.3f);
  
  JSONObject obj = JSONObject.fromObject(book);
  System.out.println(book.toString());           // renren.action.Book@10f6d3
  System.out.println(obj.toString());              // {"name":"Java","price":52.3}
 } 
}
示例二:List<Object>——>String

public class Json {
 public static void main(String[] args) {
  Book book=new Book();
  book.setName("Java");
  book.setPrice(52.3f);
  
  Book book2 = new Book();
  book2.setName("C++");
  book2.setPrice(42.3f);
  
  List list = new ArrayList();
  list.add(book);
  list.add(book2);
  
  JSONArray array = JSONArray.fromObject(list);
  System.out.println(list.toString());           // [renren.action.Book@17ee8b8, renren.action.Book@e0b6f5]
  System.out.println(array.toString());       // [{"name":"Java","price":52.3},{"name":"C++","price":42.3}]
 } 
}

 示例三:Object对象数组——>String

   Book[] book=new Book[2];  //对象数组申明
   for(int i=0; i<book.length; i++) {
   book[i] = new Book();    //对象数组初始化
  }  
   book[0].setName("Java");
   book[0].setPrice(52.3f);  
   book[1].setName("C++");
   book[1].setPrice(42.3f);
  
   JSONArray arr = JSONArray.fromObject(book);
   System.out.println(arr.toString());      // [{"name":"Java","price":52.3},{"name":"C++","price":42.3}]

2、从String到Object:先用String对象构造JSONObject对象或是JSONArray对象

示例一:

  String str = "{name:'Java',Price:52.3}"; 
  JSONObject obj = JSONObject.fromObject(str);
  System.out.println(obj.get("name") + " " + obj.get("Price"));   //Java 52.3
  System.out.println(obj.toString());        //{"name":"Java","Price":52.3}

示例二:

  String str = "[{name:'Java',price:52.3},{name:'C++',price:42.3}]"; 
  JSONArray arr = JSONArray.fromObject(str);
  for(int i=0; i<arr.size(); i++) {
   Map o = (Map)arr.get(i);
   System.out.println(o.get("name") + " " + o.get("price"));
  }                          

  //Java 52.3
  //C++ 42.3

 

3、从String到Bean

示例一:单个Bean对象

  String str = "{name:'Java',price:52.3}";
  JSONObject obj = JSONObject.fromObject(str);
  Book book = (Book)JSONObject.toBean(obj, Book.class);
  System.out.println(book.getName() + " " + book.getPrice());

示例二:Bean数组

  String str = "[{name:'Java',price:52.3},{name:'C++',price:42.3}]";
  JSONArray arr = JSONArray.fromObject(str);
  Book[] books = (Book[])JSONArray.toArray(arr, Book.class);
  for(Book b:books) {
   System.out.println(b.getName() + " " + b.getPrice());
  }

 

三、自定义封装JSON操作的类

package com.util;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonHelper {
 //从普通的Bean转换为字符串
public static String getJson(Object o){

 JSONObject jo=JSONObject.fromObject(o);
 return jo.toString();
 }
//从Java的列表转换为字符串
 public static String getJson(List list){

 JSONArray ja=JSONArray.fromObject(list);
 return ja.toString();
 }
 //从Java对象数组转换为字符串
 public static String getJson(Object[] arry){

 JSONArray ja=JSONArray.fromObject(arry);
 return ja.toString();
 }
 //从json格式的字符串转换为Map对象
 public static Map getObject(String s){

 return JSONObject.fromObject(s);
 }
 //从json格式的字符串转换为List数组
 public static List getArray(String s){

 return JSONArray.fromObject(s);
 }
 //从json格式的字符串转换为某个Bean
 public static Object getObject(String s,Class cls){

 JSONObject jo=JSONObject.fromObject(s);
 return JSONObject.toBean(jo, cls);
 }
 //从json格式的字符串转换为某类对象的数组
 public static Object getArray(String s,Class cls){

 JSONArray ja=JSONArray.fromObject(s);
 return JSONArray.toArray(ja, cls);
 }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值