例如现在有这样一个Json

String Value =  {"data":[{"school_name":"西北农林科技大学","school_id":"8"},{"school_name":"西北大学","school_id":"6"},

{"school_name":"西北工业大学","school_id":"5"},{"school_name":"西北政法大学","school_id":"2"},{"school_name":"西安交通大学","school_id":"4"},{"school_name":"西安建筑科技大学","school_id":"10"},{"school_name":"西安电子科技大学","school_id":"3"},{"school_name":"西安邮电大学","school_id":"1"},{"school_name":"长安大学","school_id":"9"},{"school_name":"陕西师范大学","school_id":"7"},{"school_name":"陕西科技大学","school_id":"11"}],"msg":"列表获取成功","status":0}

用2个数组接收school_name和school_id

首先         

             public   String[] schoolID; 
             public  String[] schoolName;

             JSONObject  jsonObj_school;//用来接收解析的JSON字符串

              JSONArray  jsonArr_school ;//用来接收JSON对象里的数组

        try{
jsonObj_school = new JSONObject ( Value ); //解析JSON字符串
jsonArr_school  =jsonObj_school.getJSONArray("data");//接收JSON对象里的数组
   int jsonSize_school = jsonArr_school.length();.//获取数组长度
   schoolID = new String[ jsonSize_school ];//初始化数组
  schoolName = new String[ jsonSize_school];//初始化数组
  for(int i = 0; i < jsonSize_school; i++ )//通过循环取出数组里的值
  {
 JSONObject jsonTemp = (JSONObject)jsonArr_school.getJSONObject(i);
   schoolID[i] = jsonTemp.getString("school_id");
   schoolName[i] = jsonTemp.getString("school_name");
   }

            }

               catch(Exception e){
e.printStackTrace();
}

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐