前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
問題由來:
最近由于做一個項目,項目的一個功能就是根據Listview的內容生成一個二維碼,然后掃描二維碼獲取list,再重新顯示listview。
核心就是:?
list—->生成二維碼——>獲取二維碼—–>獲取list
http://blog.csdn.net/demonliuhui/article/details/52948696
由于生成二維碼的參數類型是String,不能是list。如果將list的內容get出來拼成String生成二維碼。掃描二維碼后如何處理String轉為list又是一個棘手的問題。因此我想到了將list的內容封裝成json,因為json本身就是String類型,所以生成二維碼就會很簡單,進而對json進行解析生成list。整個問題就變成了一個很easy的問題:?
list—->json——->生成二維碼——>掃描二維碼獲取json——>解析json——->list
于是自己寫了一個代碼list轉換為json:
/***數據封裝成json** @param items 物料入庫數據* @return json* @throws JSONException*/
public static String GoodIn2Json(List<GoodInfo> items) throws JSONException {if (items == null) return "";JSONArray array = new JSONArray();JSONObject jsonObject = null;GoodInfo info = null;for (int i = 0; i < items.size(); i++) {info = items.get(i);jsonObject = new JSONObject();jsonObject.put(Api.COLORID, info.getColorId());jsonObject.put(Api.STOCK, info.getStock());array.put(jsonObject);}return array.toString();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 216
-
/*** 將json數組解析出來,生成自定義數據的數組* @param data 包含用戶自定義數據的json* @return 自定義信息的數據* @throws JSONException*/public static List<MoreInfo> Json2UserDefine(String data) throws JSONException {List<MoreInfo> items = new ArrayList<>();if (data.equals("")) return items;JSONArray array = new JSONArray(data);JSONObject object = null;MoreInfo item = null;for (int i = 0; i < array.length(); i++) {object = array.getJSONObject(i);String key = object.getString(Api.KEY);String value = object.getString(Api.VALUE);item = new MoreInfo(key, value);items.add(item);}return items;}
7代碼很簡單,就不詳解。這樣貌似就大功告成了,但是:?這樣只能處理list里面只有一組數據的情況。如果循環封裝成json,得到的格式就是:
[{"name":"name0","age":0}][{"name":"name1","age":5}][{"name":"name2","age":10}]
- 1
而不是:
[{"name":"name0","age":0}{"name":"name3","age":15},{"name":"name4","age":20}]
- 1
很明顯第一種格式并不是我想要的json格式,還要據循循環遍歷json解析,想想就讓人苦惱。 list里面參數少還好,如果有很多的話,豈不是要累死。
于是我百度了一下:list轉換為json,不查不知道,一查原來還有json轉換為list。狂吐兩口老血!!!?
不過也算是學到了。?
1.使用谷歌的Gson.jar。?
2.使用阿里的fastJson.jar?
已經打包好了,地址:?
http://download.csdn.net/detail/demonliuhui/9666072簡單的令人發指:
谷歌的Gson.jar
//list轉換為json Gson gson = new Gson(); List<Person> persons = new ArrayList<Person>(); String str = gson.toJson(persons);
//json轉換為list Gson gson = new Gson(); List<Person> persons = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());
阿里的fastJson.jar:
//json轉換為list//list轉換為json List<CustPhone> list = new ArrayList<CustPhone>(); String str=JSON.toJSON(list).toString();
?List<Person> list = new ArrayList<Person>(); list = JSONObject.parseArray(jasonArray, Person.class);
?
?
?
導入jar包直接調用,簡單無煩惱。
?
另一方法 :
?
?package com.listandjson;import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray;public class Test {public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("abc");list.add("123");//list轉成jsonString json =JSONArray.fromObject(list).toString();System.out.println(json); //運行:["abc","123"]//json轉成listJSONArray jsonArray = JSONArray.fromObject(json);List<String> list2 = (List) JSONArray.toCollection(jsonArray);for (int i = 0; i < list2.size(); i++) {System.out.println(list2.get(i)); //運行:abc// 123}} }
?
?