編輯:關於Android編程
/* json:{ "languages":[ {"id":1,"ide":"Eclispe","name":"java"}, {"id":2,"ide":"Xcode","name":"Swift"}, {"id":3,"ide":"Visual Studio","name":"C++"}], "cat":{"cat":"miao"} } */ public void creatJson2(){ try { JSONObject root = new JSONObject(); JSONObject cat = new JSONObject(); cat.put("cat", "miao"); JSONArray array = new JSONArray(); JSONObject lan1 = new JSONObject(); lan1.put("id", 1).put("ide", "Eclispe").put("name", "java"); JSONObject lan2 = new JSONObject(); lan2.put("id", 2).put("ide", "Xcode").put("name", "Swift"); JSONObject lan3 = new JSONObject(); lan3.put("id", 3).put("ide", "Visual Studio").put("name", "C++"); array.put(lan1); array.put(lan2); array.put(lan3); root.put("languages", array); root.put("cat", cat); System.out.println("json:"+root.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
然後是解析的代碼:
public void parseJson(){ try { InputStreamReader is = new InputStreamReader(getAssets().open("test2.json"), "UTF-8"); BufferedReader br = new BufferedReader(is); String line; StringBuilder builder = new StringBuilder(); while((line=br.readLine())!=null){ builder.append(line); } is.close();br.close(); JSONObject root = new JSONObject(builder.toString()); System.out.println("cat:"+root.getString("cat")); JSONArray array = root.getJSONArray("languages"); for(int i=0;i<array.length();i++){ JSONObject lan = array.getJSONObject(i); System.out.println(".........."); System.out.println("id="+lan.getInt("id")); System.out.println("ide="+lan.getString("ide")); System.out.println("name="+lan.getString("name")); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
這時解析的源文件:
"languages":[ {"id":1,"ide":"Eclipse","name":"java"}, {"id":2,"ide":"Xcode","name":"Swift"}, {"id":3,"ide":"Visual Studio","name":"C++"} ], "cat":"miao" }
{ 2.JSONStringer生成json Stringers only encode well-formed JSON strings. In particular: The stringer must have exactly one top-level array or object. Lexical scopes must be balanced: every call to array() must have a matching call to endArray() and every call to object() must have a matching call to endObject(). //每次調用array(),必須匹配endArray,object,endObject同理。 Arrays may not contain keys (property names). Objects must alternate keys (property names) and values. Values are inserted with either literal value calls, or by nesting arrays or objects. 它定義的所有方法: 它定義的所有方法: Public Constructors JSONStringer() Public Methods JSONStringer array() Begins encoding a new array. JSONStringer endArray() Ends encoding the current array. JSONStringer endObject() Ends encoding the current object. JSONStringer key(String name) Encodes the key (property name) to this stringer. JSONStringer object() Begins encoding a new object. String toString() Returns the encoded JSON string. JSONStringer value(double value) Encodes value to this stringer. JSONStringer value(Object value) Encodes value. JSONStringer value(long value) Encodes value to this stringer. JSONStringer value(boolean value) Encodes value to this stringer. 它的方法不多,很精簡,所以說用Stringer創建json還是很簡單的。 示例代碼:
/*json:{ "languages":[ {"id":1,"ide":"Eclispe","name":"java"}, {"id":2,"ide":"Xcode","name":"Swift"}, {"id":3,"ide":"Visual Studio","name":"C++"}], "cat":{"name":"miao"} }*/ public String createJson(){ JSONStringer stringer = new JSONStringer(); //every call to array() must have a matching call to endArray() and //every call to object() must have a matching call to endObject(). try { stringer.object(); stringer.key("languages"); stringer.array(); //數組中的三個對象 stringer.object(); stringer.key("id").value(1).key("ide").value("Eclispe").key("name").value("java"); stringer.endObject(); stringer.object(); stringer.key("id").value(2).key("ide").value("Xcode").key("name").value("Swift"); stringer.endObject(); stringer.object(); stringer.key("id").value(3).key("ide").value("Visual Studio").key("name").value("C++"); stringer.endObject(); stringer.endArray();//數組結束 stringer.key("cat"); stringer.object(); stringer.key("name").value("miao").endObject(); //結束object stringer.endObject(); System.out.println("json:"+stringer.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stringer.toString(); }
Json數據的解析與生成還是很簡單的,可以多去官網看看,多看看文檔。。。android總結任重道遠啊,寫博文就是動力啊,堅持堅持。。。。。
Buzz桌面的一大特色就是可以自己隨意更換應用程序的圖標,嫌棄官方的圖標不好看、看膩了,沒關系,咋換一個!圖標你可以自己制作,也可以下載網友分享的都行。&n
先不講那麼多看效果圖:下面來講解一些更新CODE,原理大家都知道,不廢話,直接上代碼。裡面有一些是我自己做的測試例子,所以大家可以直接刪掉就好了第一個:activity_
今天來講講自定義單個控件,就拿開關按鈕來講講,相信大家見了非常多這樣的了,先看看效果:我們可以看到一個很常見的開關按鈕,那就來分析分析。首先:這是由兩張圖片構成:①一張為
聯想旗下的控股品牌ZUK發布了新一代旗艦機ZUK Z2 Pro,那麼有網友就關心ZUK Z2 Pro有沒有NFC。NFC隨著指紋支付和APPLE PAY的盛