android 变长数据GSON解析
来源:互联网 发布:数据呈现形式 编辑:程序博客网 时间:2024/05/18 01:05
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"]}}
代码一并上传http://download.csdn.net/detail/jgszhuzhu/5514111
- android 变长数据GSON解析
- Android解析json数据(Gson)
- Android Gson解析JSON数据
- android Gson解析json数据
- Android解析Json数据之Gson解析
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android下使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android JSON数据解析(GSON方式)
- Android中使用Gson解析JSON数据
- Android中使用Gson解析数据
- Android Gson解析多层嵌套复杂数据
- Android中使用Gson解析JSON数据
- 网站的分布式架构
- SSH的jar包详解
- poj_2481Cows
- hdu 2576 数论好题 求前n项和的前n项和
- 用JQuery检查某个元素在网页上是否存在
- android 变长数据GSON解析
- window.onload的页面自动加载详解,onUnload退出当前窗口加载函数
- Wireshark图解教程
- libnet libpcap libnids libicmp介绍与使用
- 用java实现zip压缩
- Fire Net HDU && NYOJ blockhouses
- winfrom PictureBox控件显示GIF图片异常解决方案
- 用OpenSSL命令行生成证书文件
- [精通WindowsSocket网络开发-基于VC++实现]第三章——WindowsSockets基础—TCP,UDP程序