@H_502_1@
@H_502_1@具体的fastjson我就不再次介绍了不懂得看我上一篇博客http://blog.csdn.net/kluing/article/details/40982143
{ "id": "chart1","series": [{ "id": "bar1","type": "bar","groupId": "0","horizonal": false,"percentage": true,"stackType": "stack" },{ "id": "bar2",{ "id": "bar3","groupId": "1",{ "id": "line1","type": "line","yAxis": "rightAxis" }],"tools": [{ "type": "categoryTool" }] }
下面介绍如何解析assets的json数据文件
@H_502_1@
@H_502_1@先写一个解析的类,具体什么作用我都用log来解释了 ,不懂得可以看下最后的log图与解析的内容比对。
<span style="font-size:18px;">public class JsonTools { public static JSONObject jsonData(Context context,String fileName){//filename assets目录下的json文件名 JSONObject jsonDate=null; try { InputStreamReader inputStreamReader=new InputStreamReader(context.getAssets().open(fileName),"UTF-8"); BufferedReader bufferedReader=new BufferedReader(inputStreamReader); String line; StringBuilder stringBuilder=new StringBuilder(); while ((line=bufferedReader.readLine())!=null){ stringBuilder.append(line); } bufferedReader.close(); inputStreamReader.close(); jsonDate=JSON.parSEObject(stringBuilder.toString());//得到JSONobject对象 Log.e("jsonDate","=="+jsonDate); String idArray=jsonDate.getString("id");//得到id的内容 Log.e("idArray","=="+idArray); JSONArray seriesArray=jsonDate.getJSONArray("series");得到series节点,不懂得看Log打印就清楚了 Log.e("seriesArray","="+seriesArray); List<Series> listSeries=JSON.parseArray(seriesArray.toString(),Series.class); for(Series series:listSeries){ Log.e("series.getId","="+series.getId()); Log.e("series.getType","="+series.getType()); Log.e("series.getGroupId","="+series.getGroupId()); Log.e("series.getHorizonal","="+series.getHorizonal()); Log.e("series.getPercentage","="+series.getPercentage()); Log.e("series.getStackType","="+series.getStackType()); Log.e("series.getyAxis","="+series.getYAxis()); } JSONArray toolsArray=jsonDate.getJSONArray("tools");得到tools节点 List<Tools> listTools=JSON.parseArray(toolsArray.toString(),Tools.class); for(Tools tools:listTools){ Log.e("tools.getType","="+tools.getType()); } } catch (IOException e) { e.printStackTrace(); } return jsonDate; } }</span>@H_502_1@
@H_502_1@然后写3个工具类:
<span style="font-size:18px;">public class GroupData { private String id; private List<Series> seriesList=new ArrayList<Series>(); private List<Tools> toolsList=new ArrayList<Tools>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public List<Series> getSeriesList() { return seriesList; } public void setSeriesList(List<Series> seriesList) { this.seriesList = seriesList; } public List<Tools> getToolsList() { return toolsList; } public void setToolsList(List<Tools> toolsList) { this.toolsList = toolsList; } }</span>
<span style="font-size:18px;">public class Series { private String id; private String type; private String groupId; private String horizonal; private String percentage; private String stackType; private String yAxis; public String getYAxis() { return yAxis; } public void setYAxis(String yAxis) { this.yAxis = yAxis; } public String getStackType() { return stackType; } public void setStackType(String stackType) { this.stackType = stackType; } public String getPercentage() { return percentage; } public void setPercentage(String percentage) { this.percentage = percentage; } public String getHorizonal() { return horizonal; } public void setHorizonal(String horizonal) { this.horizonal = horizonal; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getId() { return id; } public void setId(String id) { this.id = id; } }</span>
<span style="font-size:18px;">public class Tools { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } }</span>
@H_502_1@最后看一下打印的Log,版面问题就只截了一部分的图。希望对大家有帮助!!
原文链接:https://www.f2er.com/json/289998.html