java怎麼讀取resources下面的JSON串文件

文件位置:
在這裏插入圖片描述

package com.jt.ceshi;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ReadJSON {

    public static void main(String[] args) {

        String laststr="";

         URL l1 =
                 Thread.currentThread().
                         getContextClassLoader().
                         getResource("ceshi.json");

        String path=String.valueOf(l1);
        //去除file:/的前綴
        path = path.replace("file:/","");
        File file=new File(path);
        BufferedReader reader=null;
        try{
            FileInputStream in = new FileInputStream(file);
            reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));// 讀取文件
            String tempString = null ;
            while((tempString=reader.readLine())!=null){
                laststr=laststr+tempString;
            }

            System.out.println("laststr:"+laststr);

            ObjectMapper mapper = new ObjectMapper();
            //json字符串映射爲map對象
            Map<String , Object>  map = new HashMap<String, Object>(16);

            //將字符串映射到對應的map
            map = mapper.readValue(laststr,map.getClass());
            //獲取id
            String id = "employees";
            System.out.println("獲取employees:"+map.get(id));
            //嵌套的對象獲取
            /*  這個辦法會報錯的。因爲是arraylist類型
           Map base = (Map)map.get(id);
           String firstName = (String)base.get("firstName");
           String twoName=(String)base.get("twoName");
           String lastName=(String)base.get("lastName");
            System.out.println(firstName+"--------"+twoName+"---------"+lastName);

*/
            ArrayList<String> list = (ArrayList<String>)map.get(id);
            System.out.println(list.toString());
           // String firstName = list.size()
//            String twoName=String.valueOf(list.get(1));
//            String lastName=String.valueOf(list.get(3));
            System.out.println();
            Object[]  listMap=list.toArray();
            System.out.println("*******************************************");
            System.out.println(listMap.toString());
            System.out.println(listMap[0].toString());
//            System.out.println(listMap[1]);
//            System.out.println(listMap[2]);
//            System.out.println(listMap[3]);
            //Map base = (Map)map.get(id);
            System.out.println(1);
            System.out.println("=============================================");
            JsonNode jsonNode = mapper.readTree(laststr);
            JsonNode  firstName = jsonNode.get("employees").get("firstName").get(0);
            System.out.println("樹形解析:"+firstName.asText());
            System.out.println("**************************************");
            
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader!=null){
                try{
                    reader.close();
                }catch(IOException el){
                }
            }
        }

    }

/*
{
    "employees": [
        {
            "firstName": "Bill",
            "twoName" : "zhuang",
            "threeName" : "jinye",
            "lastName": "Gates"
        }
    ]
}
 */

}


執行結果:返回String類型的標準json串。後面大家就可以採用工具類對String的json串,轉變標準的JSON對象了。
laststr:{ “employees”: [ { “firstName”: “Bill”, “twoName” : “zhuang”, “threeName” : “jinye”, “lastName”: “Gates” } ]}

laststr:{    "employees": [        {            "firstName": "Bill",            "twoName" : "zhuang",            "threeName" : "jinye",            "lastName": "Gates"        }    ]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章