android 读取json数据(遍历JSONObject和JSONArray)
16lz
2021-01-23
android 读取json数据(遍历JSONObject和JSONArray)
- publicStringgetJson(){
- StringjsonString="{\"FLAG\":\"flag\",\"MESSAGE\":\"SUCCESS\",\"name\":[{\"name\":\"jack\"},{\"name\":\"lucy\"}]}";//json字符串
- try{
- JSONObjectresult=newJSONObject(jsonstring);//转换为JSONObject
- intnum=result.length();
- JSONArraynameList=result.getJSONArray("name");//获取JSONArray
- intlength=nameList.length();
- Stringaa="";
- for(inti=0;i<length;i++){//遍历JSONArray
- Log.d("debugTest",Integer.toString(i));
- JSONObjectoj=nameList.getJSONObject(i);
- aa=aa+oj.getString("name")+"|";
- }
- Iterator<?>it=result.keys();
- Stringaa2="";
- Stringbb2=null;
- while(it.hasNext()){//遍历JSONObject
- bb2=(String)it.next().toString();
- aa2=aa2+result.getString(bb2);
- }
- returnaa;
- }catch(JSONExceptione){
- thrownewRuntimeException(e);
- }
- }
更多相关文章
- Android Base64字符串转换成图片
- Android Studio如何轻松整理字符串到string.xml中
- Android实现DES对字符串加密
- Android中判断字符串中必须包含字母或者数字_Android
- android 复制、粘贴字符串 (判断API 11 )
- android中遍历arrayList的四种方法
- Android实现TextView字符串波浪式跳动
- Android的数据处理:使用annotation实现JSON字符串解析为java对象