Json的解析與封裝

來源:互聯網
上載者:User

標籤:blog   http   java   os   io   檔案   for   ar   

這裡會用到http://blog.csdn.net/leejuen/article/details/38534995博文裡的讀取設定檔的方法。

>----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.codehaus.jackson.map.ObjectMapper;import net.sf.json.JSONObject;public class Main {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stub/*********************拼接Json******************************///設定必填參數Map<String,Object> params = new HashMap<String,Object>();//設定流水號params.put("merJnlNo", SystemConfig.getProperty("merJnlNo"));//設定商戶號params.put("merNo", SystemConfig.getProperty("merNo"));//設定檔案類型params.put("fileType",SystemConfig.getProperty("fileType"));//設定yyyyMMddparams.put("transDate",SystemConfig.getProperty("transDate"));//設定檔案清單List<String> fileList = new ArrayList<String>();params.put("fileList", fileList);fileList.add("file1.txt");fileList.add("file2.txt");//設定檔案名稱params.put("fileName", SystemConfig.getProperty("fileName"));//將Map轉為json格式JSONObject paramsJson = JSONObject.fromObject(params);String paramsPlaintext = paramsJson.toString();System.out.println("json format: " + paramsPlaintext);/*********************解析json******************************/ObjectMapper mapper = new ObjectMapper();//將json轉換成bean,這個bean可以是自己定義的,這裡我直接用Map類Map<String,Object >paramMap = mapper.readValue(paramsPlaintext, Map.class);System.out.println("/*****************************************************/");System.out.println("merJnlNo------->"+paramMap.get("merJnlNo"));System.out.println("merNo------->"+paramMap.get("merNo"));System.out.println("fileType------->"+paramMap.get("fileType"));System.out.println("transDate------->"+paramMap.get("transDate"));System.out.println("fileList------->"+paramMap.get("fileList"));System.out.println("fileName------->"+paramMap.get("fileName"));}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.