用Gson来解析数据
1.
数据源
首先是导入Google的Gson包

{
"resultcode":"200",/*返回标识码*/
"reason":"查询成功!",
"result":{
"company":"顺丰",
"com":"sf",
"no":"575677355677",
"list":[
{
"datetime":"2013-06-25 10:44:05", /*时间*/
"remark":"已收件", /*描述*/
"zone":"台州市" /*区域*/
},
{
"datetime":"2013-06-25 11:05:21",
"remark":"快件在 台州 ,准备送往下一站 台州集散中心 ",
"zone":"台州市"
},
{
"datetime":"2013-06-25 20:36:02",
"remark":"快件在 台州集散中心 ,准备送往下一站 台州集散中心 ",
"zone":"台州市"
},
{
"datetime":"2013-06-25 21:17:36",
"remark":"快件在 台州集散中心 ,准备送往下一站 杭州集散中心 ",
"zone":"台州市"
},
{
"datetime":"2013-06-26 12:20:00",
"remark":"快件在 杭州集散中心 ,准备送往下一站 西安集散中心 ",
"zone":"杭州市"
},
{
"datetime":"2013-06-27 05:48:42",
"remark":"快件在 西安集散中心 ,准备送往下一站 西安 ",
"zone":"西安市/咸阳市"
},
{
"datetime":"2013-06-27 08:03:03",
"remark":"正在派件..",
"zone":"西安市/咸阳市"
},
{
"datetime":"2013-06-27 08:51:33",
"remark":"派件已签收",
"zone":"西安市/咸阳市"
},
{
"datetime":"2013-06-27 08:51",
"remark":"签收人是:已签收 ",
"zone":"西安市/咸阳市"
}
],
"status": "1" /*0或1,1表示签收或退回*/
}
}
1 package com.bwf.gson_0; 2 3 import java.util.List; 4 5 public class KuaiDi { 6 int resultcode; 7 String reason; 8 Result result; 9 10 public static class Result{ 11 String company; 12 String com; 13 long no; 14 List<Details> list; 15 int status; 16 17 public static class Details{ 18 String datetime; 19 String remark; 20 String zone; 21 } 22 } 23 24 25 }
1 package com.bwf.gson_0; 2 3 import java.io.InputStream; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.widget.TextView; 12 13 import com.bwf.gson_0.KuaiDi.Result.Details; 14 import com.google.gson.Gson; 15 16 public class MainActivity extends Activity { 17 TextView tv; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 tv = (TextView) findViewById(R.id.tv); 25 } 26 27 @Override 28 public boolean onCreateOptionsMenu(Menu menu) { 29 // Inflate the menu; this adds items to the action bar if it is present. 30 getMenuInflater().inflate(R.menu.main, menu); 31 return true; 32 } 33 34 @Override 35 public boolean onOptionsItemSelected(MenuItem item) { 36 switch (item.getItemId()) { 37 case R.id.action_settings: 38 try { 39 InputStream in = getAssets().open("kuaidi.txt"); 40 byte[] buffer = new byte[1024 * 20]; 41 int num = in.read(buffer); 42 String str = new String(buffer, 0, num); 43 44 // 解析 45 Gson gson = new Gson(); 46 KuaiDi k = gson.fromJson(str, KuaiDi.class); 47 // 展示 48 showResult(k); 49 } catch (Exception e) { 50 Log.d("fanhy", "异常:" + e.toString()); 51 } 52 53 break; 54 55 default: 56 break; 57 } 58 return super.onOptionsItemSelected(item); 59 } 60 61 private void showResult(KuaiDi k) { 62 tv.append(k.resultcode + "\n" + k.reason + "\n\n"); 63 KuaiDi.Result result = k.result; 64 tv.append(result.com + "\n" + result.company + "\n" + result.no 65 + "\n" + result.status+"\n\n"); 66 List<KuaiDi.Result.Details> details = result.list; 67 for (Details details2 : details) { 68 tv.append(details2.datetime + "\n" + details2.remark + "\n" 69 + details2.zone+"\n\n"); 70 } 71 } 72 }
浙公网安备 33010602011771号