• Android Json数据解析


    1.通过主Activity的Button按钮进行解析

     1 public class MainActivity extends Activity {
     2 
     3     private Button button=null;
     4     private String jsonData="[{"name":"小张","age":25,"sex":"男"},{"name":"小李子","age":41,"sex":"女"}," +
     5             "{"name":"倩倩","age":15,"sex":"女"}]";
     6     protected void onCreate(Bundle savedInstanceState) {
     7         super.onCreate(savedInstanceState);
     8         setContentView(R.layout.main);
     9         button=(Button) super.findViewById(R.id.button);
    10         button.setOnClickListener(new OnClickListener()
    11         {
    12             public void onClick(View v)
    13             {
    14                 new JsonUtils(jsonData);
    15             }
    16         });
    17     }
    18 
    19 
    20     @Override
    21     public boolean onCreateOptionsMenu(Menu menu) {
    22         // Inflate the menu; this adds items to the action bar if it is present.
    23         getMenuInflater().inflate(R.menu.main, menu);
    24         return true;
    25     }
    26     
    27 }

    2.JosnUtils类用来解析json数据的业务类

     1 public class JsonUtils 
     2 {
     3 
     4     @SuppressLint("NewApi") public JsonUtils(String jsonData) 
     5     {
     6         try 
     7         {
     8             /**
     9              * 要想解析JSON数据之前先创建一个JsonReader对象,
    10              * 因为JsonReader对象需要一个reader参数所以要把字符串转化成StringReader对象作为参数
    11              */
    12             JsonReader jsonReader=new JsonReader(new StringReader(jsonData));
    13             //开始读取json数据
    14             jsonReader.beginArray();
    15             //循环读取json中的数据,一直到读取数据结束
    16             while(jsonReader.hasNext())
    17             {
    18                 //开始读取json对象
    19                 jsonReader.beginObject();
    20                 //循环读取json对象中的数据,一直到读取数据结束
    21                 while(jsonReader.hasNext())
    22                 {
    23                     //jsonReader.nextName()读取json对象的名称
    24                     //jsonReader.nextString()读取json对象对应的值;如果是整形对象使用jsonReader.nextInt()方法
    25                     if(jsonReader.nextName().equals("name"))
    26                     {
    27                         System.out.print("姓名:"+jsonReader.nextString());
    28                     }
    29                     if(jsonReader.nextName().equals("age"))
    30                     {
    31                         System.out.print("  年龄:"+jsonReader.nextInt());
    32                     }
    33                     if(jsonReader.nextName().equals("sex"))
    34                     {
    35                         System.out.print("  性别:"+jsonReader.nextString());
    36                     }
    37                 }
    38                 System.out.println();
    39                 //结束json对象数据的解析
    40                 jsonReader.endObject();
    41             }
    42             //结束json数据的解析
    43             jsonReader.endArray();
    44         } catch (IOException e) {
    45             // TODO Auto-generated catch block
    46             e.printStackTrace();
    47         }
    48         
    49     }
    50     
    51 }

    3.在运行程序之前要把google的gson-xxx.jar的jar包导入到类库中,下载地址:https://code.google.com/p/google-gson/downloads/list

  • 相关阅读:
    配置动态刷新RefreshScope注解使用局限性(一)
    OAuth2 Token 一定要放在请求头中吗?
    Spring Boot 2.3 新特配置文件属性跟踪
    为什么 Spring Boot 2.3.0 放弃Maven最终拥抱Gradle
    Spring Boot 2.3.0 新特性Redis 拓扑动态感应
    【spring cloud hoxton】Ribbon 真的能被 spring-cloud-loadbalancer 替代吗
    Spring Cloud Gateway 扩展支持动态限流
    聊聊 OAuth 2.0 的 token expire_in 使用
    「starter推荐」简单高效Excel 导出工具
    用mint-ui tabber写餐厅分层
  • 原文地址:https://www.cnblogs.com/tianshidechibang234/p/3189734.html
Copyright © 2020-2023  润新知