使用JSON文件

时间:2015-10-20 14:33:07

标签: java json recursion

我有两个json文件。

all_packages.json

{
  "backbone": ["jQuery", "underscore"],
  "jQuery": ["queryJ"],
  "underscore": ["lodash"],
  "queryJ": [],
  "lodash": []
}

dependencies.json

{
  "projectName": "Test0000",
  "dependencies": ["backbone"]
}

我正在尝试实现一个简单的命令行工具,该工具读取dependencies.json文件并输出应该安装的其他内容,以便一切正常。

可能已安装的软件包将位于名为installed_modules的文件夹中,其中每个依赖项都是一个单独的文件夹,该名称是软件包的名称。 该工具应该读取dependencies.json和all_packages.json并输出将要安装以下库:

示例:

Installing backbone.
In order to install backbone, we need jQuery and underscore.
Installing jQuery.
In order to install jQuery, we need queryJ.
Installing queryJ.
Installing underscore.
In order to install underscore, we need lodash. Lodash is already installed.
All done.

程序结束后,文件夹结构应该是这样的。

installed_modules/
├── backbone
├── jQuery
├── lodash
├── queryJ
└── underscore

我试图通过没有硬编码值但没有运气的递归来完成所有这些。

编辑

@William Morrison

JSONParser parser = new JSONParser();

        try {
            Object obj = parser.parse(new FileReader(".../dependencyResolving/dependencies.json"));
            JSONObject dependencies = (JSONObject) obj;

            Object obj1 = parser.parse(new FileReader("../dependencyResolving/all_packages.json"));
            JSONObject allPacks = (JSONObject) obj1;

            JSONArray deps = (JSONArray) dependencies.get("dependencies");
            Iterator<String> iterator = deps.iterator();
            while (iterator.hasNext()) {
                System.out.println("Installing " + iterator.next() + ".");
            }

            //Here i do not know how to check in all_packages which are                                   
            //dependencies and i can not create the folders.
        } catch (Exception e) {
        }

0 个答案:

没有答案