Java解析JSON对象
今天学习一下Java 解析 JSON
Game Starts
参考文档
1) www.json.org
2 ) 参考公司其他人的代码(这个就不说了)
依赖jar包
1) org.json.jar 提供以下我的网盘链接 http://pan.baidu.com/s/1jG2zSwe
主要的类
JSON主要有两种结构
1)name/value 的集合
2)value 的集合,在大多数语言中的list,array等,这里的 value 个人觉得就是一个JSON,也就是说是JSON的集合
JAVA处理JSON的类
1) JSONObject 有点类似于Map, 和JSON结构一致,key一般为String,value 不统一(string,int,array……)
2) JSONArray JSONObject数组
What\'s Up
解析网络上传过来的JSON对象怎么办(给个笨法子)
1 String url;
2 InputStream is = new URL(url).openStream();
3 BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
4 StringBuffer jsonBuffer = new StringBuffer();
5 String line;
6 while((line = br.readLine()) != null) {
7 jsonBuffer.append(line);
8 }
遇到个问题:在本机调试的很好,但是部署到服务器就不行了。
解决办法:url用utf-8处理一下,嘿嘿!
1 java.net.URLEncoder.encode(url, "utf-8")
Always Be Coding
创建JSON
1 package server;
2
3 import org.json.JSONArray;
4 import org.json.JSONException;
5 import org.json.JSONObject;
6
7 public class JSonCreator {
8 public static String getJSONString() throws JSONException {
9 JSONObject jsonObjet = new JSONObject();//书架
10 jsonObjet.put("total", 3);//3本书
11 JSONArray jsonaArray = new JSONArray();//书的集合
12 JSONObject sub = new JSONObject();//一本书
13 sub.put("bookName", "book1");//书名
14 sub.put("price", 12.0);//价格
15 sub.put("publisher", "p1");//出版社
16 jsonaArray.put(sub);
17 sub = new JSONObject();
18 sub.put("bookName", "book2");
19 sub.put("price", 22.0);
20 sub.put("publisher", "p2");
21 jsonaArray.put(sub);
22 sub = new JSONObject();
23 sub.put("bookName", "book3");
24 sub.put("price", 32.0);
25 sub.put("publisher", "p3");
26 jsonaArray.put(sub);
27 jsonObjet.put("books", jsonaArray);
28 return jsonObjet.toString();
29 }
30 public static void main(String[] args) throws JSONException {
31 System.out.println(getJSONString());
32
33 }
显示结果(自己打了几个换行)
{ "total":1234, "books":[ {"price":12,"publisher":"p1","bookName":"book1"}, {"price":22,"publisher":"p2","bookName":"book2"}, {"price":32,"publisher":"p3","bookName":"book3"} ] }
解析JSON
1 package client; 2 3 import org.json.JSONArray; 4 import org.json.JSONException; 5 import org.json.JSONObject; 6 7 import server.JSonCreator; 8 9 public class JSonParser { 10 public static void parseJSON(String json) throws JSONException { 11 12 JSONObject jsonObject = new JSONObject(json);//书架 13 System.out.println("total:"+jsonObject.getInt("total"));//数量 14 JSONArray jsonArray = jsonObject.getJSONArray("books");//书集合 15 for(int i = 0; i < jsonArray.length(); i++) { 16 JSONObject book = jsonArray.getJSONObject(i); 17 System.out.print("bookName : " + book.getString("bookName")); 18 System.out.print(" price : " + book.getDouble("price")); 19 System.out.print(" publisher : " + book.getString("publisher")); 20 System.out.println(); 21 } 22 } 23 public static void main(String[] args) throws JSONException { 24 String json = JSonCreator.getJSONString(); 25 parseJSON(json); 26 } 27 }
显示结果
total:3 bookName : book1 price : 12.0 publisher : p1 bookName : book2 price : 22.0 publisher : p2 bookName : book3 price : 32.0 publisher : p3