关闭

java Json解析和文件备份

标签: json解析java文件备份
81人阅读 评论(0) 收藏 举报
分类:
java Json解析和文件备份

1、JSON解析,这里解析采用的是org.json.JSONObject,首先看一下json文件,这里取名叫zunyijson.txt,我把其存放在E:\\xml\\test.txt下面,解析json要区分好key是JSONObject和JSONArray即可,如下:
{
    "additionalInfo": [
        {
            "name": "MAINTENANCE_PERSON_NAME",
            "value": ""
        },
        {
            "name": "REMARK",
            "value": ""
        },
        {
            "name": "IPADDRESS",
            "value": "172.31.7.8"
        },
        {
            "name": "MAINTENANCE_PERSON_ADDRESS",
            "value": ""
        },
        {
            "name": "CONTACT_ADDRESS",
            "value": "彩虹大厦南楼二层"
        },
        {
            "name": "HARDWARE_VER",
            "value": "A.2"
        },
        {
            "name": "NETYPE_DISPLAY_NAME",
            "value": "OPCOM3500E"
        },
        {
            "name": "ISTATUS",
            "value": "1"
        },
        {
            "name": "NE_CATEGORY_ID",
            "value": "0"
        },
        {
            "name": "CREATE_TIME",
            "value": "2008-12-29 11:52:59"
        },
        {
            "name": "CREATE_USER",
            "value": ""
        },
        {
            "name": "MACADDRESS",
            "value": ""
        },
        {
            "name": "READCOM",
            "value": "zydx"
        },
        {
            "name": "WRITECOM",
            "value": "zydxvip"
        },
        {
            "name": "GRID_NAME",
            "value": ""
        },
        {
            "name": "GRID_ID",
            "value": ""
        },
        {
            "name": "PROPERTY_RIGHT_UNIT",
            "value": ""
        },
        {
            "name": "CUS_NAME",
            "value": ""
        },
        {
            "name": "STATION",
            "value": ""
        },
        {
            "name": "ROOM",
            "value": ""
        },
        {
            "name": "NET_ID",
            "value": ""
        },
        {
            "name": "IS_LOCAL",
            "value": "1"
        },
        {
            "name": "TOPO_AEND_PTP",
            "value": ""
        }
    ],
    "communicationState": {},
    "emsInSyncState": true,
    "location": " ",
    "name": [
        {
            "name": "EMS",
            "value": "Raisecom/NNM"
        },
        {
            "name": "ManagedElement",
            "value": "2"
        }
    ],
    "nativeEMSName": "22-3500E-1",
    "owner": "Raisecom",
    "productName": "OPCOM3500E",
    "supportedRates": [],
    "userLabel": "22-3500E-1",
    "version": "2.5.36"
}

2、解析代码如下,文件放在本地,通过代码读取出来然后解析:

package www.zy.com.FirstJava;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 解析json
 * @author Administrator
 *
 */
public class ZunYiJson {

	public static void main(String[] args) {
		File file = new File("E:\\xml\\zunyijson.txt");
		if(!file.exists()){
			System.out.println("文件不存在!");
		}
		try {
			 analysis(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void analysis(File file) throws IOException{
		FileInputStream fis = new FileInputStream(file);
		int size = fis.available();//求出长度一次性读完
		byte[] by = new byte[size];
		fis.read(by);
		fis.close();
		String str = new String(by,"GB2312");//json字符串的结果 
		System.out.println(str);
		try {
			JSONObject jsonObject = new JSONObject(str);
			JSONArray jsonArray = jsonObject.getJSONArray("additionalInfo");
			for(int i = 0;i<jsonArray.length();i++){
				JSONObject jo = jsonArray.getJSONObject(i);
				System.out.println("name:"+ jo.get("name"));
				System.out.println("value:"+ jo.get("value"));
			}
			System.out.println("communicationState:" + jsonObject.get("communicationState"));
			System.out.println("emsInSyncState:" + jsonObject.getBoolean("emsInSyncState"));
			System.out.println("location:" + jsonObject.get("location"));
			JSONArray ja = jsonObject.getJSONArray("name");
			for(int j = 0;j<ja.length();j++){
				JSONObject js = ja.getJSONObject(j);
				System.out.println("name:" + js.get("name"));
				System.out.println("value:" + js.get("value"));
			}
			System.out.println("nativeEMSName:" + jsonObject.get("nativeEMSName"));
			System.out.println("owner:" + jsonObject.get("owner"));
			System.out.println("productName:" + jsonObject.get("productName"));
			System.out.println("supportedRates:" + jsonObject.get("supportedRates"));
			System.out.println("version:" + jsonObject.get("version"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
}



3、java实现文件备份代码,从一个路径备份到某一个路径下

	/**
	 * 备份文件到指定目录下
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	 public static void copyFile() throws FileNotFoundException, IOException {
		 	//读操作,原始目录
		 	FileInputStream	fis = new FileInputStream(PATH);//PATH是原路径
		 	if(fis!=null){
		 		 //写操作,目标目录
		        String dataTime = getStringDateShort();
		        String filePath = "F:\\ftp\\file_bak\\report" + dataTime + ".txt";//新路径
		        bakFileName = filePath;
		        FileOutputStream fos = new FileOutputStream(filePath);
		        int len = 0;
		        byte[] buf = new byte[1024];
		       
		        while ((len = fis.read(buf)) != -1) {
		            fos.write(buf, 0, len);
		        }
		        System.out.println("附件备份到F:\\ftp\\file_bak\\成功!");
		        fos.close();
		        fis.close();
		 	}
	    }
	
	/**
	 * 获取现在时间并将其格式化为指定格式 
	 * @return
	 */
	 public static String getStringDateShort() {
		   Date currentTime = new Date();
		   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		   String dateString = formatter.format(currentTime);
		   return dateString;
		}

4、json解析结果如图所示:



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15513次
    • 积分:539
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论