• JsonArray和JsonObject遍历方法


    一:遍历JsonArray

    1.  // 一个未转化的字符串  
    2. String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;   
    3.  // 首先把字符串转成 JSONArray  对象  
    4. JSONArray json = JSONArray.fromObject(str );  
    5. if(json.size()>0){  
    6.   for(int i=0;i<json.size();i++){  
    7.  // 遍历 jsonarray 数组,把每一个对象转成 json 对象  
    8. JSONObject job = json.getJSONObject(i);   
    9. // 得到 每个对象中的属性值  
    10. System.out.println(job.get("name")+"=") ;    
    11.   }  
    12. }  

    一:遍历JsonObject

    1. SONObject jsonObject = new JSONObject(s);  
    2. //然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中  
    3. JSONObject jsonObject = new JSONObject(jsonString);  
    4.         Iterator iterator = jsonObject.keys();  
    5. while(iterator.hasNext()){  
    6.             key = (String) iterator.next();  
    7.         value = jsonObject.getString(key);  
  • 相关阅读:
    Mockito一个方法的实例
    LIst与ArrayList区别
    mockito入门学习
    eclipse中调整字体大小和改变背景颜色
    常用sql语句
    eclipse导入代码和重新编译
    windows下登录linux的常用工具SecureCRT和常用命令
    junit4
    接口测试
    java环境
  • 原文地址:https://www.cnblogs.com/jon-yang/p/8818539.html
Copyright © 2020-2023  润新知