当前位置:操作系统 > 安卓/Android >>

android 变长数据GSON解析

[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":{"i

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,