1,解析javaBean集合。
{ "msg": "查询成功", "data": [<span style="white-space:pre"> </span>//json数组 { "_id": "55cda5fee24
//解析List<Bean>集合 public static <T> List<T> getBeans(String jsonSting, Class<T> cls) { List<T> list = new ArrayList<T>(); try { JSONObject jsonObject = new JSONObject(jsonSting); JSONArray datas = jsonObject.optJSONArray("data"); list = JSON.parseArray(datas.toString(), cls); } catch (Exception e) { // TODO: handle exception } return list; }
{ "data": { "pro": [ { "product_id": "156
//解析List<List<Bean>>商品的信息 public static<T> List<T> getChildBeans(String jsonString,Class<T> cls){ List<T> childArray = new ArrayList<>(); try { JSONObject jsonObject = new JSONObject(jsonString); JSONObject data = jsonObject.optJSONObject("data"); JSONArray pros = data.optJSONArray("pro"); //有两种写法.这种简单 childArray = JSON.parseArray(pros.toString() ,cls); } catch (JSONException e) { } return childArray; }
3,
/* * 11.在本地路径下读取文件的json字符串信息,得到数据并赋值javaBean对象 */ JSONReader reader = new JSONReader(new FileReader("c:\\1.txt")); // 注意type的使用 List list10 = reader.readObject(new TypeReference>() {}.getType()); reader.close();
/* * 12.将Object对象保存至本地路径中,保存为json字符串 */ JSONWriter writer = new JSONWriter(new FileWriter("c:\\2.txt")); writer.writeObject(list10); writer.close(); /*5,
/* * 13.在网络上获取json数据,并保存为对应的javaBean对象信息 */ List list11 = null; URL url = new URL("http://192.168.117.114:8080/Test/My"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); if (conn.getResponseCode() == 200) { InputStream in = conn.getInputStream(); // 通过JsonReader类得到发出的输出流对象 JSONReader reader2 = new JSONReader(new InputStreamReader(in)); // 得到Object对象 list11 = reader2.readObject(new TypeReference>(){}.getType()); } for (Person p : list11) { System.out.println(p); }6,
/* * 14.将客户端的javaBean对象,上传至服务器 */ byte[] bytes = JSON.toJSONBytes(list11); URL url2 = new URL("http://192.168.117.114:8080/Test/My"); HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection(); conn2.setRequestMethod("POST"); conn2.setConnectTimeout(3000); conn2.setRequestProperty("content-length", String.valueOf(bytes.length)); conn2.setRequestProperty("content-type","application/x-www-form-urlencoded"); conn2.setDoOutput(true); OutputStream os = conn2.getOutputStream(); os.write(bytes, 0, bytes.length); os.close(); if (conn2.getResponseCode() == 200) { System.out.println("上传成功! "); }
3.格式:
解析方法:就是一层一层解析。
try { JSONObject jsonObject = new JSONObject(result); // JSONObject data = jsonObject.optJSONObject("arr"); JSONObject dataArray = jsonObject.optJSONObject("data");
JSONArray jsonArray = dataArray.optJSONArray("0");
tvR.setText(jsonArray.getString(1));//------------------解析3-------
JSONObject jsonObject3 = dataArray.optJSONObject("3"); JSONArray jsonArray1 = jsonObject3.optJSONArray("create_time"); jsonPas(jsonArray1,0);
public void jsonPas(JSONArray jsonArray,int c) throws JSONException { if (jsonArray.length() > 0){ judgeShow(jsonArray.getString(2),jsonArray.getString(0),jsonArray.getString(1),c); } }
public void judgeShow(String flag,String rou,String time,int c){ if (flag.equals("0")){ for (int i = 0; i < tv0.length; i++) { if (i == c) { tv0[i].setText(rou); tv0[i].setTextColor(Color.parseColor("#666666")); }
如果需要对比长度:可以这样
if (jsonObject3.length() > 5) {
参考:http://bbs.9ria.com/thread-248297-1-1.html
感谢原作者