Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 變長數據GSON解析

android 變長數據GSON解析

編輯:關於Android編程

[java]
package com.example.gsonparse; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.lang.reflect.Field; 
import java.util.LinkedHashMap; 
import java.util.Map.Entry; 
 
import org.apache.http.util.EncodingUtils; 
 
import com.google.gson.Gson; 
 
import android.os.Bundle; 
import android.R.string; 
import android.app.Activity; 
import android.view.Menu; 
 
public class MainActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  try { 
   InputStream inputStream = getApplicationContext().getAssets().open( 
     "data.txt"); 
   String string =getInputStream(inputStream); 
   parseData(string); 
 
  } catch (IOException e) { 
   // TODO Auto-generated catch block  
   e.printStackTrace(); 
  } 
 
 } 
 
 private String  getInputStream(InputStream inputStream) { 
  try { 
   String encoding = "UTF-8"; // 字符編碼(可解決中文亂碼問題 )  
   InputStreamReader read = new InputStreamReader(inputStream, 
     encoding); 
   BufferedReader bufferedReader = new BufferedReader(read); 
   String lineTXT = null; 
   while ((lineTXT = bufferedReader.readLine()) != null) { 
    System.out.println(lineTXT.toString().trim()); 
    return lineTXT.toString().trim(); 
   } 
   read.close(); 
  } catch (Exception e) { 
   System.out.println("讀取文件內容操作出錯"); 
   e.printStackTrace(); 
  } 
  return null; 
 } 
  
 private void parseData(String string){ 
  Gson  gson  = new Gson(); 
  DataBean dataBean  =  gson.fromJson(string, DataBean.class); 
  Industry industry  = dataBean.data; 
   
 String[] string2= industry.stockType; 
 for (int i = 0; i < string2.length; i++) { 
  System.out.println(string2[i]); 
 } 
   
 LinkedHashMap<String, String> hashMap  = industry.industry; 
 for (Entry<String, String> entry : hashMap.entrySet()) { 
  String string3  =  entry.getValue(); 
  System.out.println(string3); 
 } 
  
  
 } 
 

 
 
  
 
構建對應的類,注意方法名 
 
package com.example.gsonparse; 
 
public class DataBean { 
 public String message; 
 public Industry data; 
 public int status; 
 

 
 
  
 
  
 
package com.example.gsonparse; 
 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.LinkedHashMap; 
 
public class Industry { 
 public LinkedHashMap<String, String> industry; 
 public String[] stockType; 
 
  
  

 
 
  

package com.example.gsonparse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.apache.http.util.EncodingUtils;

import com.google.gson.Gson;

import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  try {
   InputStream inputStream = getApplicationContext().getAssets().open(
     "data.txt");
   String string =getInputStream(inputStream);
   parseData(string);

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 private String  getInputStream(InputStream inputStream) {
  try {
   String encoding = "UTF-8"; // 字符編碼(可解決中文亂碼問題 )
   InputStreamReader read = new InputStreamReader(inputStream,
     encoding);
   BufferedReader bufferedReader = new BufferedReader(read);
   String lineTXT = null;
   while ((lineTXT = bufferedReader.readLine()) != null) {
    System.out.println(lineTXT.toString().trim());
    return lineTXT.toString().trim();
   }
   read.close();
  } catch (Exception e) {
   System.out.println("讀取文件內容操作出錯");
   e.printStackTrace();
  }
  return null;
 }
 
 private void parseData(String string){
  Gson  gson  = new Gson();
  DataBean dataBean  =  gson.fromJson(string, DataBean.class);
  Industry industry  = dataBean.data;
 
 String[] string2= industry.stockType;
 for (int i = 0; i < string2.length; i++) {
  System.out.println(string2[i]);
 }
 
 LinkedHashMap<String, String> hashMap  = industry.industry;
 for (Entry<String, String> entry : hashMap.entrySet()) {
  String string3  =  entry.getValue();
  System.out.println(string3);
 }
 
 
 }

}


 

構建對應的類,注意方法名

package com.example.gsonparse;

public class DataBean {
 public String message;
 public Industry data;
 public int status;

}


 

 

package com.example.gsonparse;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class Industry {
 public LinkedHashMap<String, String> industry;
 public String[] stockType;

 
 
}


 


首先看原數據

{"status":1,"message":"\u8bf7\u6c42\u6570\u636e\u6210\u529f","data":{"industry":{"1":"\u519c\u3001\u6797\u3001\u7267\u3001\u6e14\u4e1a","2":"\u91c7\u6398\u4e1a","3":"\u98df\u54c1\u3001\u996e\u6599","4":"\u7eba\u7ec7\u3001\u670d\u88c5\u3001\u76ae\u6bdb","5":"\u6728\u6750\u3001\u5bb6\u5177","6":"\u9020\u7eb8\u3001\u5370\u5237","7":"\u77f3\u6cb9\u3001\u5316\u5b66\u3001\u5851\u80f6\u3001\u5851\u6599","8":"\u7535\u5b50","9":"\u91d1\u5c5e\u3001\u975e\u91d1\u5c5e","10":"\u673a\u68b0\u3001\u8bbe\u5907\u3001\u4eea\u8868","11":"\u533b\u836f\u3001\u751f\u7269\u5236\u54c1","12":"\u5176\u4ed6\u5236\u9020\u4e1a","13":" \u7535\u529b\u3001\u7164\u6c14\u53ca\u6c34\u7684\u751f\u4ea7\u548c\u4f9b\u5e94\u4e1a\r\n","14":"\u5efa\u7b51\u4e1a\r\n","15":"\u4ea4\u901a\u8fd0\u8f93\u3001\u4ed3\u50a8\u4e1a\r\n","16":"\u4fe1\u606f\u6280\u672f\u4e1a\r\n","17":"\u6279\u53d1\u548c\u96f6\u552e\u8d38\u6613\r\n","18":"\u91d1\u878d\u3001\u4fdd\u9669\u4e1a\r\n","19":"\u623f\u5730\u4ea7\u4e1a\r\n","20":"\u793e\u4f1a\u670d\u52a1\u4e1a\r\n","21":"\u4f20\u64ad\u4e0e\u6587\u5316\u4ea7\u4e1a\r\n","22":"\u7efc\u5408\u7c7b\r\n"},"stockType":["\u5168\u90e8","\u6caa\u6df1A\u80a1","\u521b\u4e1a\u677f"]}}

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved