今天做个任务,需要json数据解析。以前只做过Gson 解析,代码如下:
Gson gson=new Gson(); ArrayList<News> list=gson.fromJson(arg0, new TypeToken<ArrayList<News>>() {}.getType());
json的源数据:list转化成JSONArray.
NewsInfoDAO list=new NewsInfoDAO(); List<News> listNews=list.getAllNews(type); JSONArray array=JSONArray.fromObject(listNews); out.print(array.toString());
通过gson解析成list。
今天做的任务由于不知道源json数据的类型 json数据: {“Code”:1,"Data":{}} 用Gson没解析出来。经查阅资料得:一种简单的json数据解析方法:
JSONObject 和JSONArray
Data类:
public class Data { private int UID; private String Key; private int Agent; private String Agent_Key; private int Store; private String Store_Key; public int getUID() { return UID; } public void setUID(int uID) { UID = uID; } public String getKey() { return Key; } public void setKey(String key) { Key = key; } public int getAgent() { return Agent; } public void setAgent(int agent) { Agent = agent; } public String getAgent_Key() { return Agent_Key; } public void setAgent_Key(String agent_Key) { Agent_Key = agent_Key; } public int getStore() { return Store; } public void setStore(int store) { Store = store; } public String getStore_Key() { return Store_Key; } public void setStore_Key(String store_Key) { Store_Key = store_Key; } public Data(int uID, String key, int agent, String agent_Key, int store, String store_Key) { super(); UID = uID; Key = key; Agent = agent; Agent_Key = agent_Key; Store = store; Store_Key = store_Key; } public Data() { super(); } }
客户端:用volley请求队列:
queue=Volley.newRequestQueue(MainActivity.this); CustomRequest request=new CustomRequest(Method.POST, "http://api.hht365.net/Member/Verify.php", new Listener<String>() { @Override public void onResponse(String arg0) JSONObject jsonObject; try { jsonObject = new JSONObject(arg0); co = jsonObject.getInt("Code"); data=(Data)jsonObject.get("Data"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } switch(co){ case 0: handler.sendEmptyMessage(0); break; case 1: handler.sendEmptyMessage(1); break; case 2: handler.sendEmptyMessage(2); break; case 3: handler.sendEmptyMessage(3); break; case 4: handler.sendEmptyMessage(4); break; case 5: handler.sendEmptyMessage(5); break; case 6: handler.sendEmptyMessage(6); break; default: break; } } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub handler.sendEmptyMessage(7); } }); request.setParams("username", username.getText().toString().trim()); request.setParams("password", password.getText().toString().trim()); request.setParams("code", code.getText().toString().trim()); request.setParams("cid", String.valueOf(cid)); request.setParams("role", role); queue.add(request); } }); }
JSONObject jsonObject = new JSONObject(json);获取服务端的json数据
int code = jsonObject.optString("code");获取code对象;
Data data=(Data)jsonObject.get("Data");获取Data对象;
这样json数据就解析出来了。
总结:json数据的解析:
基本方法介绍:
1. List集合转换成json方法
List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray = JSONArray.fromObject( list );
2. Map集合转换成json方法
Map map = new HashMap(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map);
3. Bean转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );
6.beans转换成json代码
List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list);
演示示例
package com.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * 使用json-lib构造和解析Json数据 * * @author Alexia * @date 2013/5/23 * */ public class JsonTest { /** * 构造Json数据 * * @return */ public static String BuildJson() { // JSON格式数据解析对象 JSONObject jo = new JSONObject(); // 下面构造两个map、一个list和一个Employee对象 Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "Alexia"); map1.put("sex", "female"); map1.put("age", "23"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("name", "Edward"); map2.put("sex", "male"); map2.put("age", "24"); List<Map> list = new ArrayList<Map>(); list.add(map1); list.add(map2); Employee employee = new Employee(); employee.setName("wjl"); employee.setSex("female"); employee.setAge(24); // 将Map转换为JSONArray数据 JSONArray ja1 = JSONArray.fromObject(map1); // 将List转换为JSONArray数据 JSONArray ja2 = JSONArray.fromObject(list); // 将Bean转换为JSONArray数据 JSONArray ja3 = JSONArray.fromObject(employee); System.out.println("JSONArray对象数据格式:"); System.out.println(ja1.toString()); System.out.println(ja2.toString()); System.out.println(ja3.toString()); // 构造Json数据,包括一个map和一个Employee对象 jo.put("map", ja1); jo.put("employee", ja2); System.out.println("\n最终构造的JSON数据格式:"); System.out.println(jo.toString()); return jo.toString(); } /** * 解析Json数据 * * @param jsonString Json数据字符串 */ public static void ParseJson(String jsonString) { // 以employee为例解析,map类似 JSONObject jb = JSONObject.fromObject(jsonString); JSONArray ja = jb.getJSONArray("employee"); List<Employee> empList = new ArrayList<Employee>(); // 循环添加Employee对象(可能有多个) for (int i = 0; i < ja.size(); i++) { Employee employee = new Employee(); employee.setName(ja.getJSONObject(i).getString("name")); employee.setSex(ja.getJSONObject(i).getString("sex")); employee.setAge(ja.getJSONObject(i).getInt("age")); empList.add(employee); } System.out.println("\n将Json数据转换为Employee对象:"); for (int i = 0; i < empList.size(); i++) { Employee emp = empList.get(i); System.out.println("name: " + emp.getName() + " sex: " + emp.getSex() + " age: " + emp.getAge()); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ParseJson(BuildJson()); } }
时间: 2024-11-06 21:58:23