JSONObject 解析对象的类
利用JSON类得到JSON字符串:
构造方法:
|--JSONObject(String source): 把一个字符串映射成对应的JSONObject对象。
|--JSONObject(Map map):把一个map集合映射成对应的JSONObject对象
|--JSONObject():构造一个空的JSONObject对象
|--JSONObject(Object bean):把任意对象转变成json对象 ----bean必须定义在自己的文件中
常用方法:
|--get***(String name):根据name获得对应的值,如果不存在则抛出异常(***代表boolean double int long string)
|--opt***(String name):根据name获得对应的值,如果不存在则返回null, 0, false(***代表boolean
double int long string)
|--opt***(String name, *** value):根据name获得对应的值,如果不存在则返回value(***代表boolean
double int long string)
|--put(String key, *** value): 设置对象中的键值对,如果已经存在key则覆盖
|--getJSONObject(String name):根据name获得对应的JSONObject对象
|--getJSONArray(String name):根据name获得相应的JSONArray对象
JSONArray 解析数组的类
构造方法:
|--JSONArray(Collection collection):把集合映射成对应的JSONArray对象
|--JSONArray(String source):把字符串转成一个JSON对象
常用方法:
|--getJSONObject(int index):根据对应的下标返回一个JSONObject对象
|--getJSONArray(int index):根据对应的下标返回一个JSONArray对象
|--length():返回当前JSONArray里面对象的个数
|--get | opt***():同上
代码示意:
解析字符串代码:
import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONObject;/*** 利用JSON解析** @author Mixm* @date 2015年10月10日 上午11:37:45*/public class Test02 {public static void main(String[] args) throws Exception {String source = "{'name':'Mixm', 'gender':'男','age':21, 'hobby': [{'name':'basketball'}, {'name':'football'}]}";JSONObject jObject = new JSONObject(source);System.out.println(jObject);Person p = new Person();p.setName(jObject.getString("name"));p.setGender(jObject.getString("gender"));p.setAge(jObject.getInt("age"));JSONArray jArray = jObject.getJSONArray("hobby");Hobby hobby = new Hobby();List<String> list = new ArrayList<>();for (int i = 0; i < jArray.length(); i++) {JSONObject obj = jArray.getJSONObject(i);list.add(obj.getString("name"));}hobby.setList(list);p.setHobby(hobby);System.out.println(p);}}
Person里自定义的属性:
private String name;private String gender;private int age;private Hobby hobby;
Hobby类属性:
private List<String> list;
解析网络上的JSON:
1、先利用网络请求获得字符串
2、解析字符串
import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class JSONParser {public static void main(String[] args) throws Exception {String path = "http://api2.hichao.com/new_forum/status?ga/new_forum/status&forum_ids=&gv=72&gi=e4b6f3f836bdb59c&access_token=&gos=5.5.2&p=Galaxy+S2+-+4.1.1+-+API+16+-+480x800_1&gc=wandoujia&gn=mxyc_adr&gf=android&gs=";JSOND jd = getData(path);System.out.println(jd);}public static JSOND getData(String path) throws JSONException {JSONObject jsonObject1 = new JSONObject(getJSON(path));JSOND jd = new JSOND();jd.setMessage(jsonObject1.getString("message"));Datas data = new Datas();JSONObject jsonObject2 = jsonObject1.getJSONObject("data");List<Item> list = new ArrayList<>();JSONArray jArray = jsonObject2.getJSONArray("items");for (int i = 0; i < jArray.length(); i++) {Item item = new Item();JSONObject jsonObject3 = jArray.getJSONObject(i);item.setCommentCount(jsonObject3.getString("commentCount"));item.setId(jsonObject3.getString("id"));item.setName(jsonObject3.getString("name"));item.setOnline(jsonObject3.getString("online"));item.setPicUrl(jsonObject3.getString("picUrl"));item.setThreadCount(jsonObject3.getString("threadCount"));list.add(item);}data.setList(list);data.setAppApi(jsonObject2.getString("appApi"));jd.setData(data);return jd;}public static String getJSON(String path){URL url;String str = "";try {url = new URL (path);HttpURLConnection connection = (HttpURLConnection) url.openConnection();BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));String temp = null;while ((temp=br.readLine()) != null) {str += temp;temp = null;}} catch (Exception e) {e.printStackTrace();}return str;}}
JSOND类定义的属性:
private String message;private Datas datas;
Datas类定义的属性:
private List<Item> list;private String appApi;
Items类定义的属性:
private String name;private String threadCount;private String commentCount;private String picUrl;private String online;private String id;
利用JSON类得到JSON字符串:
1、使用JSONObject,对javabean和map生成
import java.util.HashMap;import java.util.Map;import org.json.JSONObject;public class CreateJson01 {public static void main(String[] args) {TrainInfo ti = new TrainInfo("mixm","2012-10-01","forever", "guess", "never", "closed");JSONObject jObject1 = new JSONObject(ti);System.out.println(jObject1);Map<Integer, TrainInfo> maps = new HashMap<>();maps.put(1, ti);maps.put(2, ti);maps.put(3, ti);maps.put(4, ti);JSONObject jObject2 = new JSONObject(maps);System.out.println(jObject2);}}
2、使用JSONArray,对list集合生成
import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONObject;public class CreateJson02 {public static void main(String[] args) {TrainInfo ti1 = new TrainInfo("mixm1","2012-10-01","forever", "guess", "never", "closed");TrainInfo ti2 = new TrainInfo("mixm2","2013-10-01","forever", "guess", "never", "closed");TrainInfo ti3 = new TrainInfo("mixm3","2014-10-01","forever", "guess", "never", "closed");TrainInfo ti4 = new TrainInfo("mixm4","2015-10-01","forever", "guess", "never", "closed");JSONObject jObject1 = new JSONObject(ti1);System.out.println(jObject1);List<TrainInfo> list = new ArrayList<>();list.add( ti1);list.add( ti2);list.add( ti3);list.add( ti4);JSONArray jObject2 = new JSONArray(list);System.out.println(jObject2);}}

2867

被折叠的 条评论
为什么被折叠?



