編輯:關於Android編程
Android原生json操作 是通過JSONObject 和JSONArray兩個對象操作的
解析json字符串 對象轉為json字符串解析json字符串
json字符串
{ "name":"張三", "age":19, "id":0, "live":true, "interest":["basketball","computer","programmer"] }
實體類
/** * Created by sky-mxc */ public class Student { private String name; private int id; private int age; private boolean live; private String[] interests; }
使用JSONObject 解析
/** * 解析 json字符串 */ private void parserJsonStr() { try { //得到json字符串 String json = getJsonStr(getAssets().open("object.json")); //封裝為json對象 JSONObject jo = new JSONObject(json); //逐一獲取屬性 String name = jo.getString("name"); int age = jo.getInt("age"); int id = jo.getInt("id"); boolean live = jo.getBoolean("live"); JSONArray ja = jo.getJSONArray("interest"); String [] interest = new String[ja.length()]; for (int i=0;i<ja.length();i++){ interest[i]="ja.getString(i)+",";" }="" student="" stu="new" student(name,id,age,live,interest);="" log.e("tag","="==="+stu.toString());" catch="" (ioexception="" e)="" {="" e.printstacktrace();="" (jsonexception="" **="" *="" 讀取json字符串="" @param="" is="" 輸入流="" @return="" json字符串="" @throws="" ioexception="" public="" string="" getjsonstr(inputstream="" is)="" throws="" stringbuffer="" json="new" stringbuffer();="" inputstreamreader="" isr="new" inputstreamreader(is,"utf-8");="" bufferedreader="" reader="new" bufferedreader(isr);="" line="null;" while="" ((line="reader.readLine())!=null){" json.append(line);="" return="" json.tostring();=""
將對象轉為json字符串
實體類
/** * Created by sky-mxc */ public class Clazz { private String name; private Liststudents; }
轉換
/** * 將list集合轉為json字符串 */ private void listToJson() { Clazz clazz = new Clazz(); clazz.setName("sky"); Liststus = new ArrayList<>(); Student stu1 = new Student("張三",1,19,true,new String[]{"song","sport","swimming"}); Student stu2 = new Student("李四",1,20,true,new String[]{"song","sleep","swimming"}); Student stu3 = new Student("王五",1,39,false,new String[]{"drink","sport","swimming"}); stus.add(stu1); stus.add(stu2); stus.add(stu3); clazz.setStudents(stus); //轉為json字符串 JSONObject jo = new JSONObject(); try { jo.put("name",clazz.getName()); JSONArray ja = new JSONArray(); for (Student stu:stus){ JSONObject j = new JSONObject(); j.put("name",stu.getName()); j.put("age",stu.getAge()); j.put("live",stu.isLive()); JSONArray jsonArray = new JSONArray(); for(int i=0;i 結果:
{ "name":"sky", "students":[ { "name":"張三", "age":19, "live":true, "interest":[ "song", "sport", "swimming" ] }, { "name":"李四", "age":20, "live":true, "interest":[ "song", "sleep", "swimming" ] }, { "name":"王五", "age":39, "live":false, "interest":[ "drink", "sport", "swimming" ] } ] }
原生json操作真的好繁瑣 。我還是使用的fastjson較多一點
1、修改hosts文件,這個方法操作簡單,但經常失靈,或許運氣不好吧。 首先更新host文件,如圖,打開目錄 C:WindowsSystem32driverse
比如對方發的語音消息聽不到/沒聲音。這可是十分突出的一個問題,那麼該如何解決呢?微信沒有聲音是怎麼回事?微信沒有聲音提示怎麼辦呢?微信沒有聲音怎麼辦?一起看
今天電腦usb接口出問題了,就想著可不可以通過wifi連上手機,然後進行eclipse調試,看logcat等等。 網上搜了一下,果然有這種解決方案。
前言1.由於粘貼了較大的代碼,造成內容比較長,可能會花費您較長的時間。2.項目裡面沒有做權限判斷,所以如果發現有頁面發生崩潰可能是權限沒有打開,請打開權限後再進行嘗試。3