Json解析本地json文件

最近遇到一个问题,将本地的Json文件解析出来。

下面我将实现思路和有关代码给大家一一列出来。

1、Json定义:

一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。

2、json解析需要引入下列库

Json解析本地json文件_第1张图片

3、json文件的准备:

我这里采用的是.txt文件保存的json文件。大家编辑的json文件是否正确可以在下面这个网址检测

Json在线编辑

下面是我自己的txt文件

{
    "InkCaseApp":
	[
        {
            "softName": "EpiReader",
            "softUrl": "http://download.inkcase.com/InkCaseApps/EpiReader.apk",
            "softSize":"11.91M",
            "softIcon":"http://cn.inkcase.com/app/img/icon_epireader.png",
            "versionId": "1",
            "softPackage":"com.gajah.",
            "softDescription": "可以将你手机上的阅读页面发送到InkCase上显示"
 
        },
        {
            "softName": "InkCase Photo",
	    "softUrl": "http://download.inkcase.com/InkCaseApps/InkCasePhoto.apk",
	    "softSize":"2.39M",
    	    "softIcon":"http://cn.inkcase.com/app/img/icon_photo.png",
            "versionId": "1",
            "softPackage":"com.gajah.",
            "softDescription": "可以把你手机里的图片和照片发送到InkCase上显示,通过InkCase Photo,您也可以拍摄照片并立刻发送带InkCase。"
       
        },
        {
            "softName": "InkCase Sports",
	    "softUrl": "http://download.inkcase.com/InkCaseApps/InkCaseSport2014-8-21_1.0.10.apk",
	    "softSize":"2.04M",
	    "softIcon":"http://cn.inkcase.com/app/img/plus/Sports-ok3.png",
            "versionId": "1",
            "softPackage":"com.gajah.",
            "softDescription": ""
        
        },
        {
            "softName": "iReader",
	    "softUrl": "http://download.inkcase.com/InkCaseApps/ireader.apk",
	    "softSize":"2.99M",
    	    "softIcon":"http://cn.inkcase.com/app/img/icon_ireader.png",
            "versionId": "1",
            "softPackage":"com.gajah.",
            "softDescription": ""
        }
	]
}
大家可以自定义自己的文件,只要格式符合Json语法即可。

4、代码实现部分:

首先看看代码类:


从类名大家基本上可以知道每个类的用途:

AppInfo:封装App信息的类

public class AppInfo {

	public AppInfo() {
		super();
	}

	private String softName;
	private String softUrl;
	private double softSize;
	private String softIcon;
	private int versionId;
	private String softPackage;
	private String softDescription;
	public String getSoftName() {
		return softName;
	}
	public void setSoftName(String softName) {
		this.softName = softName;
	}
	public String getSoftUrl() {
		return softUrl;
	}
	public void setSoftUrl(String softUrl) {
		this.softUrl = softUrl;
	}
	public double getSoftSize() {
		return softSize;
	}
	public void setSoftSize(double softSize) {
		this.softSize = softSize;
	}
	
	/*
	 * public String toString(){ return this.softName + "," + this.softUrl + ","
	 * + this.softSize; }
	 */
	public String getSoftIcon() {
		return softIcon;
	}
	public void setSoftIcon(String softIcon) {
		this.softIcon = softIcon;
	}
	public int getVersionId() {
		return versionId;
	}
	public void setVersionId(int versionId) {
		this.versionId = versionId;
	}
	public String getSoftPackage() {
		return softPackage;
	}
	public void setSoftPackage(String softPackage) {
		this.softPackage = softPackage;
	}
	public String getSoftDescription() {
		return softDescription;
	}
	public void setSoftDescription(String softDescription) {
		this.softDescription = softDescription;
	}

}

JsonAnalysisTxt:解析实现的部分。代码很简单,Json实现的类就那几个,大家只要入手写一遍就都清楚了。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonAnalysisTXT {

	public static void main(String[] args) {

		/*
		 * String json =
		 * "{'softName':'Inkcase','softUrl':'http://luckytjx.blog.163.com/blog/static/140809827201262505578/','softSize':'10.20M'}"
		 * ; AppInfo appInfo = new AppInfo(); JSONObject obj =
		 * JSONObject.fromObject(json); appInfo = (AppInfo)
		 * JSONObject.toBean(obj, AppInfo.class); System.out.println(appInfo);
		 */

		// 从JSon文件读取数据
		StringBuffer stringBuffer = new StringBuffer();
		String line = null;
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(new File(
					"C:\\Users\\guojian\\Desktop\\InkCase.txt")));
			while ((line = br.readLine()) != null) {
				stringBuffer.append(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} // 将Json文件数据形成JSONObject对象 JSONObject
		JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
		JSONArray appArray = jsonObject.getJSONArray("InkCaseApp");
		List appList = new ArrayList();
		for (int i = 0; i < appArray.size(); i++) {
			System.out.println("-------------");
			AppInfo appInfo = (AppInfo) JSONObject.toBean(
					JSONArray.fromObject(appArray.toString()).getJSONObject(i),
					AppInfo.class);
			appList.add(appInfo);
			System.out.println("-------添加成功!------");
		}
		
		String name = "";
		String url = "";
		String version = "";
		String description = "";
		String icon = "";
		String size = "";
		String softPackage = "";
		StringBuffer jsonFileInfo = new StringBuffer();
		System.out.println("------------------------");
		for (int i = 0; i < appList.size(); i++) {
			name = JSONArray.fromObject(appArray.toString()).getJSONObject(i)
					.getString("softName");
			url = JSONArray.fromObject(appArray.toString()).getJSONObject(i)
					.getString("softUrl");
			version = JSONArray.fromObject(appArray.toString())
					.getJSONObject(i).getString("versionId");
			description = JSONArray.fromObject(appArray.toString())
					.getJSONObject(i).getString("softDescription");
			softPackage = JSONArray.fromObject(appArray.toString())
					.getJSONObject(i).getString("softPackage");
			size = JSONArray.fromObject(appArray.toString()).getJSONObject(i)
					.getString("softSize");
			jsonFileInfo.append("\nsoftName:" + name + "\nurl:" + url
					+ "\nversion:" + version + "\nfunctionldescription:"
					+ description + "\nicon:" + icon + "\nsize:" + size
					+ "\nsoftPackage:" + softPackage);
		}
		System.out.println(jsonFileInfo);

	}
}

惯例来个有图有真相,效果图:

Json解析本地json文件_第2张图片




你可能感兴趣的:(java,Json,txt,jsonObject)