java包路径解析成json

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java包路径解析成json相关的知识,希望对你有一定的参考价值。

List<String> list = Arrays.asList("com","com.xxxxx","com.xxxxx.risklist",
"com.xxxxx.risklist.xom","com.xxxxx.risklist.xom.domain",
"com.xxxxx.risklist.xom.domain.bean","com.xxxxx.risklist.xom.domain.dao",
"com.xxxxx.risklist.xom.domain.fundinterestratebean",
"com.xxxxx.risklist.xom.domain.impl","com.xxxxx.risklist.xom.domain.storeaccessbean",
"com.xxxxx.risklist.xom.storeaccess","com.xxxxx.risklist.xom.util");

如何转化成
"@name":"com","Package":"@name":"com.shenma","Package":"@name":"com.shenma.risklist","Package":"@name":"com.shenma.risklist.xom","Package":["@name":"com.shenma.risklist.xom.storeaccess","Package":,"@name":"com.shenma.risklist.xom.util","Package":,"@name":"com.shenma.risklist.xom.domain","Package":["@name":"com.shenma.risklist.xom.domain.impl","Package":,"@name":"com.shenma.risklist.xom.domain.storeaccessbean","Package":,"@name":"com.shenma.risklist.xom.domain.dao","Package":"@name":"com.shenma.risklist.xom.domain.fundinterestratebean","Package":,"@name":"com.shenma.risklist.xom.domain.bean","Package":]]

参考技术A

我跟你讲一种方式,我刚刚想了一下,你最好是在得到你list数据的时候就开始组装,这样是最好的,得到list数据无非是文件递归嘛,当递归到出口的时候,你就放进去,如果是文件夹也往里放。

代码刚写好了。

import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;


/**
 * @Package com.xhxh.zhidao
 * @Description json convert
 * @Author xiehua
 * @Date 2019-03-08 16:28
 */
public class FileDomain 
    private String name;
    private List<FileDomain> packages = new LinkedList<>();

    public FileDomain() 
    

    public FileDomain(String name) 
        this.name = name;
    

    public List<FileDomain> getPackages() 
        return packages;
    

    public String getName() 
        return name;

    

    public void setName(String name) 
        this.name = name;
    

    @Override
    public String toString() 
        return "FileDomain" +
                "name='" + name + '\\'' +
                ", packages=" + packages +
                '';
    

    public static void main(String[] args) 
        FileDomain fileDomain = new FileDomain("com.xhxh");
        fileDomain.readFiles("com.xhxh", fileDomain);
        String s = JSONObject.toJSONString(fileDomain);
        System.out.println(s);
    

    public void readFiles(String scanPackage, FileDomain fileDomain) 
        URL url = this.getClass().getClassLoader().getResource(scanPackage.replaceAll("\\\\.", "/"));
        File classDir = new File(url.getFile());
        for (File file :
                classDir.listFiles()) 
            if (file.isDirectory()) 
                FileDomain temFileDomain = new FileDomain(scanPackage + "." + file.getName());
                fileDomain.getPackages().add(temFileDomain);
                readFiles(scanPackage + "." + file.getName(), temFileDomain);
             else 
                FileDomain temFileDomain = new FileDomain(scanPackage + "." + file.getName());
                fileDomain.getPackages().add(temFileDomain);
            
        
    

jacksonall的使用,解析json

转自:http://www.cnblogs.com/lee0oo0/archive/2012/08/23/2652751.html ,

Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。可以参考:

Jackson 框架,轻易转换JSON

首先需要下载包:jackson-databind-2.0.5.jar jackson-annotations-2.0.5.jar jackson-core-2.0.5.jar,网址在:http://wiki.fasterxml.com/JacksonDownload 

custom.json:

{
    "country":"USA",   
    "countryInfo":{"golden":40,"silver":23,"copper":33,"tank":1},  
"countryDetail":[  
    {"id":"no","header":"号码","fieldIndex":"no","sortOrder":null,"printable":true},  
    {"id":"name","header":"姓名","fieldIndex":"name","sortOrder":null,"printable":true},  
    {"id":"total","header":"总成绩","fieldIndex":"total","sortOrder":null,"printable":true},  
    {"id":"detail","header":"详细信息","fieldIndex":"detail","sortOrder":null,"printable":true}  
   ], 
"orgood":true}  

Java的Bean及解析: 

  1 public class Bean {
  2 
  3     private String country;
  4     private boolean orgood;
  5     private Country countryInfo;
  6     private CountryDetail[] countryDetail;
  7 
  8     public String getCountry() {
  9         return country;
 10     }
 11 
 12     public void setCountry(String country) {
 13         this.country = country;
 14     }
 15 
 16     public boolean isOrgood() {
 17         return orgood;
 18     }
 19 
 20     public void setOrgood(boolean orgood) {
 21         this.orgood = orgood;
 22     }
 23 
 24     public Country getCountryInfo() {
 25         return countryInfo;
 26     }
 27 
 28     public void setCountryInfo(Country countryInfo) {
 29         this.countryInfo = countryInfo;
 30     }
 31 
 32     public CountryDetail[] getCountryDetail() {
 33         return countryDetail;
 34     }
 35 
 36     public void setCountryDetail(CountryDetail[] countryDetail) {
 37         this.countryDetail = countryDetail;
 38     }
 39 
 40     public static class Country {
 41         public String getGolden() {
 42             return golden;
 43         }
 44 
 45         public void setGolden(String golden) {
 46             this.golden = golden;
 47         }
 48 
 49         public String getSilver() {
 50             return silver;
 51         }
 52 
 53         public void setSilver(String silver) {
 54             this.silver = silver;
 55         }
 56 
 57         public String getCopper() {
 58             return copper;
 59         }
 60 
 61         public void setCopper(String copper) {
 62             this.copper = copper;
 63         }
 64 
 65         public String getTank() {
 66             return tank;
 67         }
 68 
 69         public void setTank(String tank) {
 70             this.tank = tank;
 71         }
 72 
 73         private String golden;
 74         private String silver;
 75         private String copper;
 76         private String tank;
 77 
 78     }
 79 
 80     public static class CountryDetail {
 81         public String getId() {
 82             return id;
 83         }
 84 
 85         public void setId(String id) {
 86             this.id = id;
 87         }
 88 
 89         public String getHeader() {
 90             return header;
 91         }
 92 
 93         public void setHeader(String header) {
 94             this.header = header;
 95         }
 96 
 97         public String getFieldIndex() {
 98             return fieldIndex;
 99         }
100 
101         public void setFieldIndex(String fieldIndex) {
102             this.fieldIndex = fieldIndex;
103         }
104 
105         public String getSortOrder() {
106             return sortOrder;
107         }
108 
109         public void setSortOrder(String sortOrder) {
110             this.sortOrder = sortOrder;
111         }
112 
113         public boolean isPrintable() {
114             return printable;
115         }
116 
117         public void setPrintable(boolean printable) {
118             this.printable = printable;
119         }
120 
121         private String id;
122         private String header;
123         private String fieldIndex;
124         private String sortOrder;
125         private boolean printable;
126 
127     }
128 
129     public static void main(String args[]) {
130         ObjectMapper mapper = new ObjectMapper();
131         try {
132             Bean bean = mapper.readValue(new File("custom.json"), Bean.class); // 读取
133                                                                                 // System.out.println("Country:"+bean.getCountry());
134             System.out.println("CountryInfo:" + bean.getCountryInfo().getGolden());
135             System.out.println("CountryDetail:" + bean.getCountryDetail()[1].getHeader());
136             System.out.println("orgood:" + bean.isOrgood());
137         } catch (JsonParseException e) {
138             // TODO Auto-generated catch block
139             e.printStackTrace();
140         } catch (JsonMappingException e) {
141             // TODO Auto-generated catch block
142             e.printStackTrace();
143         } catch (IOException e) {
144             // TODO Auto-generated catch block
145             e.printStackTrace();
146         }
147     }
148 }

 

以上是关于java包路径解析成json的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis 配置多路径XML,多路径接口包扫描

如何使用路径中文件中的数组序列化 JSON 数组?

JsonPath学习笔记

json-路径解析

java.lang.IllegalStateException:应为 BEGIN_ARRAY,但路径为 BEGIN_OBJECT

加jar包啥时候需要添加类路径