现在的位置: 首页 > 综合 > 正文

JSON复杂文档解析 Android自带JSONObject,JSONArray方法

2018年04月01日 ⁄ 综合 ⁄ 共 7726字 ⁄ 字号 评论关闭

解析的文档内容为:
{
  "responseCode": 0,
  "msgInfo": "业务处理成功。",
  "msgDetailInfo": null,
  "data": [
    {
      "pbtId": 1,
      "pbtName": "公积金",
      "keyGroupList": [
        {
          "keyGroupId": 1,
          "itemId": 10001,
          "keyList": [
            {
              "keyValueId": 1,
              "cityCode": "3501",
              "cityName": "福州",
              "keyName": "housFundAccount",
              "keyText": "账号",
              "keyDesc": "公积金的账号",
              "keyValue": "350104198001019999",
              "isPrivate": "0"
            },
            {
              "keyValueId": 2,
              "cityCode": "3501",
              "cityName": "福州",
              "keyName": "housFundPassword",
              "keyText": "密码",
              "keyDesc": "公积金的密码",
              "keyValue": "9999",
              "isPrivate": "1"
            }
          ]
        },
        {
          "keyGroupId": 2,
          "itemId": 10002,
          "keyList": [
            {
              "keyValueId": 3,
              "cityCode": "3502",
              "cityName": "厦门",
              "keyName": "housFundAccount",
              "keyText": "账号",
              "keyDesc": "公积金的账号",
              "keyValue": "350204198001018888",
              "isPrivate": "0"
            },
            {
              "keyValueId": 4,
              "cityCode": "3502",
              "cityName": "厦门",
              "keyName": "housFundPassword",
              "keyText": "密码",
              "keyDesc": "公积金的密码",
              "keyValue": "8888",
              "isPrivate": "1"
            }
          ]
        }
      ]
    },
    {
      "pbtId": 2,
      "pbtName": "医保",
      "keyGroupList": [
        {
          "keyGroupId": 3,
          "itemId": 10003,
          "keyList": [
            {
              "keyValueId": 5,
              "cityCode": "3501",
              "cityName": "福州",
              "keyName": "mediInsuranceAccount",
              "keyText": "账号",
              "keyDesc": "医保的账号",
              "keyValue": "350104198001019999",
              "isPrivate": "0"
            },
            {
              "keyValueId": 6,
              "cityCode": "3501",
              "cityName": "福州",
              "keyName": "mediInsurancePassword",
              "keyText": "密码",
              "keyDesc": "医保的密码",
              "keyValue": "111111",
              "isPrivate": "1"
            }
          ]
        },
        {
          "keyGroupId": 4,
          "itemId": 10004,
          "keyList": [
            {
              "keyValueId": 7,
              "cityCode": "3502",
              "cityName": "厦门",
              "keyName": "mediInsuranceAccount",
              "keyText": "账号",
              "keyDesc": "医保的账号",
              "keyValue": "350204198001018888",
              "isPrivate": "0"
            },
            {
              "keyValueId": 8,
              "cityCode": "3502",
              "cityName": "厦门",
              "keyName": "mediInsurancePassword",
              "keyText": "密码",
              "keyDesc": "医保的密码",
              "keyValue": "000000",
              "isPrivate": "1"
            }
          ]
        }
      ]
    }
  ]
}
 
解析步骤:
第一步:创建不同的javaBean用于存储每一层的属性值;
第二步:若是JSON文档存放在Assets中,获取文档的内容存在在String中
 /**
  * 获取文件流
  * @param context
  * @return
  * @throws IOException
  */
 public InputStream getInputStream(Context context,String fileName) throws IOException{
  
  AssetManager am=context.getAssets();
  InputStream is=am.open(fileName);
  return is;
 }
 /**
  * 将文件中的内容,保存在String中
  * @param is
  * @return
  * @throws IOException
  */
 public String getFileData(InputStream is) throws IOException{
  
  BufferedReader br=new BufferedReader(new InputStreamReader(is));
  StringBuilder sb=new StringBuilder();
  String str;
  while((str=br.readLine())!=null){
   sb.append(str);
  }
  return sb.toString();
 }
 /**
  * 获取文件资源内容
  * @param context
  * @param fileName
  * @return
  * @throws IOException
  */
 public String getFileData(Context context,String fileName) throws IOException{
  FileUtil fileUtil=new FileUtil();
  InputStream is=fileUtil.getInputStream(context, fileName);
  return fileUtil.getFileData(is);
 }
第三步:解析文件
private ArrayList<Business> arrayListBusiness = new ArrayList<Business>();
 private ArrayList<Datas> arrayListDatas = new ArrayList<Datas>();
 private ArrayList<KeyGroupList> arrayListKeyGroupList = new ArrayList<KeyGroupList>();
 private ArrayList<KeyList> arrayListKeyList = new ArrayList<KeyList>();
 
 private Business business;
 private Datas datas;
 private KeyGroupList keyGroupList;
 private KeyList keyList;
/**
  * 用Android自带的JSONObject和JSONArray方法
  * @throws IOException
  */
 public void getData1() throws IOException {
 
  String strData = fileUtil.getFileData(this, "test.txt");
  StringReader sr = new StringReader(strData);
  JsonReader jr = new JsonReader(sr);
  jr.beginObject();
  business = new Business();
  if (jr.nextName().contains("responseCode")) {
   int responseCode = jr.nextInt();
   Log.v("responseCode", "" + responseCode);
   business.setResponseCode(responseCode);
  }
  if (jr.nextName().equals("msgInfo")) {
   String msgInfo = jr.nextString();
   Log.v("msgInfo", msgInfo);
   business.setMsgInfo(msgInfo);
  }
  if (jr.nextName().equals("msgDetailInfo")) {
   String msgDetailInfo = jr.nextString();
   Log.v("msgDetailInfo", msgDetailInfo);
   business.setMsgDetailInfo(msgDetailInfo);
  }
  if (jr.nextName().equals("data")) {
   jr.beginArray();
   while (jr.hasNext()) {
    datas = new Datas();
    jr.beginObject();
    if (jr.nextName().equals("pbtId")) {
     int pbtId = jr.nextInt();
     Log.v("pbtId", "" + pbtId);
     datas.setPbtId(pbtId);
    }
    if (jr.nextName().equals("pbtName")) {
     String pbtName = jr.nextString();
     Log.v("pbtName", "" + pbtName);
     datas.setPbtName(pbtName);
    }
    if (jr.nextName().equals("keyGroupList")) {
     jr.beginArray();
     while (jr.hasNext()) {
      keyGroupList = new KeyGroupList();
      jr.beginObject();
      if (jr.nextName().equals("keyGroupId")) {
       int keyGroupId = jr.nextInt();
       Log.v("keyGroupId", "" + keyGroupId);
       keyGroupList.setKeyGroupId(keyGroupId);
      }
      if (jr.nextName().equals("itemId")) {
       String itemId = jr.nextString();
       Log.v("itemId", itemId);
       keyGroupList.setItemId(itemId);
      }
      if (jr.nextName().equals("keyList")) {
       jr.beginArray();
       while (jr.hasNext()) {
        keyList = new KeyList();
        jr.beginObject();
        if (jr.nextName().equals("keyValueId")) {
         int keyValueId = jr.nextInt();
         Log.v("keyValueId", "" + keyValueId);
         keyList.setKeyValueId(keyValueId);
        }
        if (jr.nextName().equals("cityCode")) {
         String cityCode = jr.nextString();
         Log.v("cityCode", cityCode);
         keyList.setCityCode(cityCode);
        }
        if (jr.nextName().equals("cityName")) {
         String cityName = jr.nextString();
         Log.v("cityName", cityName);
         keyList.setCityName(cityName);
        }
        if (jr.nextName().equals("keyName")) {
         String keyName = jr.nextString();
         Log.v("keyName", keyName);
         keyList.setKeyName(keyName);
        }
        if (jr.nextName().equals("keyText")) {
         String keyText = jr.nextString();
         Log.v("keyText", keyText);
         keyList.setKeyText(keyText);
        }
        if (jr.nextName().equals("keyDesc")) {
         String keyDesc = jr.nextString();
         Log.v("keyDesc", keyDesc);
         keyList.setKeyDesc(keyDesc);
        }
        if (jr.nextName().equals("keyValue")) {
         String keyValue = jr.nextString();
         Log.v("keyValue", keyValue);
         keyList.setKeyValue(keyValue);
        }
        if (jr.nextName().equals("isPrivate")) {
         int isPrivate = jr.nextInt();
         Log.v("isPrivate", "" + isPrivate);
         keyList.setIsPrivate(isPrivate);
        }
        jr.endObject();
        arrayListKeyList.add(keyList);
       }
 
       jr.endArray();
       keyGroupList.setArrayListKeyList(arrayListKeyList);
      }
      jr.endObject();
      arrayListKeyGroupList.add(keyGroupList);
     }
     jr.endArray();
     datas.setArrayListKeyGroupList(arrayListKeyGroupList);
    }
    jr.endObject();
    arrayListDatas.add(datas);
   }
   jr.endArray();
   business.setArrayListDatas(arrayListDatas);
  }
  jr.endObject();
  arrayListBusiness.add(business);
 }

抱歉!评论已关闭.