标签:gson
Gson是一种对象的解析json,很好用,介绍一个网站http://json.parser.online.fr/可以帮我们看一个字符串是不是Json
对于Json文件
{ "id" : "3232", "data" : { "data1" : { "name" : "xiaoming", "age" : "12" } } }
public class MyData { int <strong>id</strong>; Data <strong>data</strong>; } public class Data { Data1 <strong>data1</strong>; } public class Data1 { String <strong>name</strong>; String <strong>age</strong>; }
我们把json文件放在assets下,解析的时候这样写:
public void parseAssertData() { InputStream is = null; try { is = this.getAssets().open("ss.json", Context.MODE_PRIVATE); int length = is.available(); byte[] buffer = new byte[length]; is.read(buffer); String temp = new String(buffer); Reader response = new StringReader(temp.toString()); Gson gson = new Gson(); MyData mydata = gson.fromJson(response,MyData.class); System.out.println("===age="+mydata.data.data1.age); } catch (IOException ex) { ex.printStackTrace(); } }
标签:gson
原文地址:http://blog.csdn.net/baidu_nod/article/details/37883869