解析Json

在网页中想后台传递多个数据时,有时数据还是多个动态列表,数据很复杂时,JavaScript程序员喜欢把他们作为json串进行处理,后台收到后需要对json字符串进行解析,幸好有JSON-lib,这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:http://json-lib.sourceforge.net/

 

 今天自己写了一个JSON的例子如下,可以一调用就出了问题,报下面这个异常:

java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

上网上搜了一下才知道原来在使用JSON的使用的时候,不光要导入JSON这个包,同时还要导入另外三个包

总之,共需要下面的 jars,json-lib方可使用:
commons-beanutils-1.7.0.jar 
commons-lang-2.1.jar 
ezmorph-1.0.2.jar 
json-lib-1.1-jdk15.jar

 

 

   

例子如下:

package json;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class Test {

/**
*
* @param args
*
* @author wen
*/

public static void main(String[] args) {

// test1();

// test2();

String json = "{"
+ "1:"
+ "{"
+ "1:"
+ "{jhinfo:"
+ "['计划一','知行网','www.zhixing123.cn'],"
+ "jhrate:"
+ "['1-5:10.0','6-100:5.0/1']"
+ "},"
+ "2:"
+ "{jhinfo:"
+ "['计划二','知行网','www.zhixing123.cn'],"
+ "jhrate:"
+ "['1-100:100.0']},"
+ "3:{jhinfo:['计划三','知行网','www.zhixing123.cn'],jhrate:['1-100:150.0/7']}"
+ "},"
+ "2:{"
+ "4:"
+ "{jhinfo:['年计划','知行网','www.zhixing123.cn'],"
+ "jhrate:['365-365:1000.0']}"
+ "}"
+ "}";

try {

JSONObject jsonObject = JSONObject.fromObject(json);

String name = jsonObject.getString("1");

String address = jsonObject.getString("2");

System.out.println("name is:" + name);

System.out.println("address is:" + address);

Iterator it = jsonObject.keys();

while (it.hasNext()) {

System.out.println(jsonObject.get(it.next()));

}
test2();
test1();

} catch (Exception e) {

e.printStackTrace();

}

}

/**
*
* json对象,解析出数组
*
* @author wen
*/

private static void test2() {

String json = "{'name': '亲亲宝宝','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'亲亲宝宝'}";

try {

JSONObject jsonObject = JSONObject.fromObject(json);

String name = jsonObject.getString("name");

String address = jsonObject.getString("address");

System.out.println("name is:" + name);

System.out.println("address is:" + address);

JSONArray jsonArray = jsonObject.getJSONArray("array");

for (int i = 0; i < jsonArray.size(); i++) {

System.out.println("item " + i + " :" + jsonArray.getString(i));

}

} catch (JSONException e) {

e.printStackTrace();

}

}

/**
*
* json数组 转换,数组以[开头
*
* @author wen
*/

private static void test1() {

boolean[] boolArray = new boolean[] { true, false, true };

JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

System.out.println(jsonArray1);

// prints [true,false,true]

List list = new ArrayList();

list.add("first");

list.add("second");

JSONArray jsonArray2 = JSONArray.fromObject(list);

System.out.println(jsonArray2);

// prints ["first","second"]

JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");

System.out.println(jsonArray3);

// prints ["json","is","easy"]

}
}

你可能感兴趣的:(解析Json)