fastjson解析本地json数据二

前端之家收集整理的这篇文章主要介绍了fastjson解析本地json数据二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



具体的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数据文件

先写一个解析的类,具体什么作用我都用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>

然后写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>


这里工具类,具体怎么写这个是需要根据我们的json数据的格式来写的。还有就是用的时候不要忘了导入有关fastjson的jar。

最后看一下打印的Log,版面问题就只截了一部分的图。希望对大家有帮助!!

猜你在找的Json相关文章