Java数据解析---JSON
一、Java数据解析分为:XML解析和JSON解析
XML解析即是对XML文件中的数据解析,而JSON解析即对规定形式的数据解析,比XML解析更加方便
JSON解析基于两种结构:
1、键值对类型
1 { 2 “name”:”Alice”, 3 “age”:19 4 }
2、数组类型(有序列表)
1 { 2 “students”: 3 [ 4 {“name”:”Alice”,“age”:19}, 5 {“name”:”Ben”,”age”:11} 6 ] 7 }
JSON解析依赖于外部jar包。
首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:http://json-lib.sourceforge.net/)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
二、JSON解析字符串
1、先看一个最简单的字符串数据
1 String json = "{name:'Alice',age:19,sex:'gril'}";
例如,对于这样一个字符串数据进行JSON解析,要解析的数据即是冒号(:)后面的数据 Alice 19 gril
把{}包含的所有看作是一个对象,把[]包含的看作为一个数组,分清JSONObject 和JSONArray 多练习 就掌握了
1 package demo; 2 3 import org.json.JSONException; 4 import org.json.JSONObject; 5 6 public class Demo01 { 7 public static void main(String[] args) throws JSONException { 8 String json = "{name:'Alice',age:19,sex:'gril'}"; 9 //将{}里面的整体看作一个对象 10 JSONObject obj = new JSONObject(json); 11 //根据键名(:前面的)获得值(:后面的) 12 String name = obj.getString("name"); 13 int age = obj.getInt("age"); 14 String sex = obj.getString("sex"); 15 16 System.out.println("name:"+name+",age:"+age+",sex:"+sex); 17 } 18 }
2、再看一个对象中含对象的
1 String json = "{student:{name:'Lucy',age:19,sex:'gril'}}";
1 package demo; 2 3 import org.json.JSONException; 4 import org.json.JSONObject; 5 6 public class Demo01 { 7 8 public static void main(String[] args) throws JSONException { 9 String json = "{student:{name:'Lucy',age:19,sex:'gril'}}"; 10 JSONObject obj = new JSONObject(json); 11 //注意点 12 JSONObject stu =obj.getJSONObject("student"); 13 String name = stu.getString("name"); 14 int age = stu.getInt("age"); 15 String sex = stu.getString("sex"); 16 17 System.out.println("name:"+name+",age:"+age+",sex:"+sex); 18 } 19 }
3、再看字符串中含有数组类型的情况
1 String json = "{students:[{address:'山东',age:10,name:'小明'},{address:'河南',age:15,name:'小李'}]}";
1 package json; 2 3 import org.json.JSONArray; 4 import org.json.JSONException; 5 import org.json.JSONObject; 6 7 public class Demo03 { 8 public static void main(String[] args) throws JSONException { 9 String json = "{students:[{address:'山东',age:10,name:'小明'},{address:'河南',age:15,name:'小李'}]}"; 10 JSONObject obj = new JSONObject(json); 11 JSONArray stu = obj.getJSONArray("students"); 12 for(int i=0;i<stu.length();i++) 13 { 14 JSONObject s = stu.getJSONObject(i); 15 String name = s.getString("name"); 16 int age = s.getInt("age"); 17 String address = s.getString("address"); 18 19 System.out.println("第"+i+"个对象"+"name:"+name+",age:"+age+",address:"+address); 20 21 } 22 } 23 } 24 25 26 第0个对象name:小明,age:10,address:山东 27 第1个对象name:小李,age:15,address:河南
4、看几个发杂点的字符串JSON解析
1 String json = "{object:{persons:[{name:'小明',image:'帅'},{name:'小李',image:'高'},{name:'小王',image:'富'}]}}";
1 package json; 2 3 import org.json.JSONArray; 4 import org.json.JSONException; 5 import org.json.JSONObject; 6 7 public class Demo04 { 8 public static void main(String[] args) throws JSONException { 9 String json = "{object:{persons:[{name:'小明',image:'帅'},{name:'小李',image:'高'},{name:'小王',image:'富'}]}}"; 10 JSONObject obj = new JSONObject(json); 11 JSONObject object = obj.getJSONObject("object"); 12 // JSONObject person = object.getJSONObject("persons"); 13 JSONArray p = object.getJSONArray("persons"); 14 for(int i=0;i<p.length();i++) 15 { 16 JSONObject person = p.getJSONObject(i); 17 String name = person.getString("name"); 18 String image = person.getString("image"); 19 System.out.println("第"+(i+1)+"个对象的name:"+name+",image:"+image); 20 } 21 22 } 23 } 24 25 26 第1个对象的name:小明,image:帅 27 第2个对象的name:小李,image:高 28 第3个对象的name:小王,image:富
5、
1 String json = "[{school:'清华',good:'false',class:{name:'物理系',count:100}},{school:'北大',good:'true',class:{name:'化学系',count:45}}]";
1 package json; 2 3 4 5 import org.json.JSONArray; 6 import org.json.JSONException; 7 import org.json.JSONObject; 8 9 public class Demo05 { 10 public static void main(String[] args) throws JSONException { 11 String json = "[{school:'清华',good:'false',class:{name:'物理系',count:100}},{school:'北大',good:'true',class:{name:'化学系',count:45}}]"; 12 JSONArray arr = new JSONArray(json); 13 for(int i=0;i<arr.length();i++) 14 { 15 JSONObject ss = arr.getJSONObject(i); 16 String school = ss.getString("school"); 17 String good = ss.getString("good"); 18 JSONObject clas = ss.getJSONObject("class"); 19 String name = clas.getString("name"); 20 String count = clas.getString("count"); 21 System.out.println("school:"+school+",good:"+good+",className:"+name+",classCount:"+count); 22 } 23 } 24 } 25 26 27 28 school:清华,good:false,className:物理系,classCount:100 29 school:北大,good:true,className:化学系,classCount:45
6、
1 String json ="{status:2,result:[{name:'小明',id:1}, {name:'小红',id:5},{name:'小王',id:7}]} " ;
1 package json; 2 3 import javax.naming.spi.DirStateFactory.Result; 4 5 import org.json.JSONArray; 6 import org.json.JSONException; 7 import org.json.JSONObject; 8 9 public class Demo06 { 10 public static void main(String[] args) throws JSONException { 11 String json ="{status:2,result:[{name:'小明',id:1}, {name:'小红',id:5},{name:'小王',id:7}]} " ; 12 JSONObject obj = new JSONObject(json); 13 String status = obj.getString("status"); 14 JSONArray result = obj.getJSONArray("result"); 15 for(int i=0;i<result.length();i++) 16 { 17 JSONObject com = result.getJSONObject(i); 18 String name = com.getString("name"); 19 String id = com.getString("id"); 20 System.out.println("name:"+name+",id:"+id); 21 22 } 23 24 25 26 } 27 } 28 29 30 31 name:小明,id:1 32 name:小红,id:5 33 name:小王,id:7
三、解析网络中的数据
步骤:
1、得到指定网络地址上的数据的输入流,并存储到内存流
2、将内存流中的数据转换成字符串
3、对字符串进行JSON解析,并将数据存入到集合中
1 import java.io.IOException; 2 import java.util.List; 3 4 import org.json.JSONException; 5 6 public class Test { 7 8 public static void main(String[] args) throws IOException, JSONException { 9 String path = "http://..."; //数据源 10 11 //得到json字符串 12 String jsonString = HttpUtil.getJsonString(path); 13 //把json字符串数据解析成集合 14 List<T> list = HttpUtil.jsonParser(jsonString); 15 //遍历集合 16 for(T t:list) 17 { 18 System.out.println(t); 19 } 20 21 } 22 23 }
1 import java.io.ByteArrayOutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.net.HttpURLConnection; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.json.JSONArray; 11 import org.json.JSONException; 12 import org.json.JSONObject; 13 14 public class HttpUtil { 15 16 //得到服务器端的json字符串 17 public static String getJsonString(String path) throws IOException 18 { 19 URL url = new URL(path); 20 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 21 22 conn.setRequestMethod("GET"); 23 conn.setConnectTimeout(5000); 24 conn.setDoInput(true); 25 InputStream in = null; 26 if(conn.getResponseCode()==200) 27 { 28 in = conn.getInputStream(); 29 return changeToString(in); 30 } 31 return null; 32 } 33 34 private static String changeToString(InputStream in) throws IOException { 35 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 36 byte[] arr = new byte[1024]; 37 int len = 0; 38 while((len = in.read(arr))!=-1) 39 { 40 //写入内存流 41 bos.write(arr,0,len); 42 } 43 //转换成字符串形式返回 44 return new String(bos.toByteArray(),"utf-8"); 45 } 46 47 //解析json字符串, 48 public static List<T> jsonParser(String jsonString) throws JSONException 49 { 50 List<T> list = new ArrayList<T>(); 51 JSONObject obj = new JSONObject(jsonString); 52 //依据实际情况进行JSON解析 53 return list; //多个人的信息 54 55 } 56 57 } 58 59 60 61 62
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。