복잡한 플러그인 형식 json 데이터 변환

5123 단어 json
필요: 복잡한 형식의 문자열을 지정한 순서대로value를 출력합니다
json   :
{
	"UnicomWarning": {
		"Version": "1.0",
		"TestFlag": "1",
		"AreaCode": "",
		"RecordCount": "1",
		"Warnings": [{
			"WarnId": "00001",
			"WarnName": "    ",
			"CreateTime": "2018-11-08 14:00:00",
			"RemoveTime": null,
			"ComplainNum": 0,
			"WarnContent": "      ",
			"BusiType": "05",
			"BusiSubType": null,
			"ServType": "010103",
			"OccurRange": "19",
			"CityCode": "190",
			"WarnState": "01",
			"WarnLevel": "1",
			"WarnDesc": "RULE0001",
			"WarnObject": "1  "
		}, {
			"WarnId": "00001",
			"WarnName": "    ",
			"CreateTime": "2018-11-08 14:00:00",
			"RemoveTime": "2018-11-08 14:30:00",
			"ComplainNum": 0,
			"WarnContent": "      ",
			"BusiType": "05",
			"BusiSubType": null,
			"ServType": "010103",
			"OccurRange": "19",
			"CityCode": "190",
			"WarnState": "01",
			"WarnLevel": "1",
			"WarnDesc": "RULE0001",
			"WarnObject": "1  "
		}]
	}
}
     json      bejson: http://www.bejson.com
        :
"WarnId","WarnName","CreateTime","RemoveTime","ComplainNum","WarnContent","BusiType","BusiSubType","ServType","OccurRange","CityCode","WarnState","WarnLevel","WarnDesc","WarnObject"
    :
00001    2018-11-08 14:00:00null0      05null01010319190011RULE00011  

사용할 json 도구jar 패키지:
net.sf.json.JSONArray;
net.sf.json.JSONObject;
  :
package com.pz.json.utils;

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

public class JsonTest {
	public static void main(String[] args) {
		//  orderKeys     , json  value    
		String[] orderKeys = {"WarnId","WarnName","CreateTime","RemoveTime","ComplainNum","WarnContent","BusiType","BusiSubType","ServType","OccurRange","CityCode","WarnState","WarnLevel","WarnDesc","WarnObject"};
		//     value,      json   
		String body  = "{\"UnicomWarning\":{\"Version\":\"1.0\",\"TestFlag\":\"1\",\"AreaCode\":\"a\",\"RecordCount\":\"1\",\"Warnings\":[{\"WarnId\":1,\"WarnName\":\"    \",\"CreateTime\":\"2018-11-08 14:00:00\",\"RemoveTime\":null,\"ComplainNum\":0,\"WarnContent\":\"      \",\"BusiType\":\"05\",\"BusiSubType\":null,\"ServType\":\"010103\",\"OccurRange\":\"19\",\"CityCode\":\"190\",\"WarnState\":\"01\",\"WarnLevel\":\"1\",\"WarnDesc\":\"RULE0001\",\"WarnObject\":\"1  \"},{\"WarnId\":\"00001\",\"WarnName\":\"    \",\"CreateTime\":\"2018-11-08 14:00:00\",\"RemoveTime\":\"2018-11-08 14:30:00\",\"ComplainNum\":0,\"WarnContent\":\"      \",\"BusiType\":\"05\",\"BusiSubType\":null,\"ServType\":\"010103\",\"OccurRange\":\"19\",\"CityCode\":\"190\",\"WarnState\":\"01\",\"WarnLevel\":\"1\",\"WarnDesc\":\"RULE0001\",\"WarnObject\":\"1  \"}]}}";
		JSONObject jsonObject =JSONObject.fromObject(body);  
		//  json  UnicomWarning
		JSONObject data = jsonObject.getJSONObject("UnicomWarning");    
		//  json  Warnings
		JSONArray jsonArray = data.getJSONArray("Warnings");  
		
		JSONObject row = null;
		StringBuffer finalStr = new StringBuffer();
		//  value      
		byte[] b = {0x01};
		String separatorStr = new String(b) ;//value      
		String lineBreaker = "\r
";// for (int i = 0; i < jsonArray.size(); i++) { row = jsonArray.getJSONObject(i); //System.out.println(row); for (String keyStr : orderKeys) { Object keyStr_v = ""; if(row.has(keyStr)) { keyStr_v = row.get(keyStr).equals(null)?"":row.get(keyStr); } finalStr.append(keyStr_v.toString()).append(separatorStr); } finalStr.append(data.get("AreaCode")).append(lineBreaker); } System.out.println(finalStr); } }

출력 형식:
1    2018-11-08 14:00:000      0501010319190011RULE00011  a
00001    2018-11-08 14:00:002018-11-08 14:30:000      0501010319190011RULE00011  a

좋은 웹페이지 즐겨찾기