Android Json数据解析

来源:互联网 发布:灰色预测软件2007 编辑:程序博客网 时间:2024/06/06 19:38


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     @Override21     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 block46             e.printStackTrace();47         }48         49     }50     51 }
复制代码

 

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

0 0
原创粉丝点击