android 變長資料GSON解析

來源:互聯網
上載者:User

[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"]}}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.