Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android原生json操作 -- Android學習之路

Android原生json操作 -- Android學習之路

編輯:關於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)+&quot;,&quot;;" }="" student="" stu="new" student(name,id,age,live,interest);="" log.e("tag","="===&quot;+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 List students;
}

轉換

 /**
   * 將list集合轉為json字符串
   */
  private void listToJson() {
      Clazz clazz  = new Clazz();
      clazz.setName("sky");
      List stus = 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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved