简体   繁体   English

JSONArray转换为可用的Java / String数组

[英]JSONArray to usable Java/String array

This is not a duplicate 这不是重复的

My JSON is not in an JSONArray, My JSON is all objects. 我的JSON不在JSONArray中,我的JSON是所有对象。 However I have passed the objects into an JSONArray as it was the only way I could figure out how to get each objects in 'patches'. 但是,我已经将对象传递到JSONArray中,因为这是我弄清楚如何在“补丁”中获取每个对象的唯一方法。

Right now I have everything converted to a JSONArray but I am unable to use it like I would like. 现在,我已将所有内容转换为JSONArray,但无法按我的意愿使用它。 The end goal of this is to take the JSON from the server end (Which is dynamic by the way, the patch files under patches get regenerated and new patches are added) Eventually I want to take that array list and use it to search for those packages in the users directory, but thats for later. 最终目的是从服务器端获取JSON(顺便说一下,这是动态的,将重新生成补丁下的补丁文件,并添加新的补丁)。最终,我想获取该数组列表并使用它来搜索那些软件包放在用户目录中,但是那以后再用。

Right now I can't figure out how to convert data out of the JSONArray. 现在,我不知道如何将数据转换出JSONArray。

The JSON is currently structured as follows on the server end. JSON当前在服务器端的结构如下。 (This structure CANNOT BE CHANGED). (无法更改此结构)。

{  
   "patches":{  
      "patch1.zip":{  
         "name":"patch1.zip",
         "type":"file",
         "path":"patch1.zip",
         "size":15445899,
         "checksum":"ed4e2275ba67470d472c228a78df9897"
      },
      "patch2.zip":{  
         "name":"patch2.zip",
         "type":"file",
         "path":"patch2.zip",
         "size":1802040,
         "checksum":"59de97037e5398c5f0938ce49a3fa200"
      },
      "patch3.zip":{  
         "name":"patch3.zip",
         "type":"file",
         "path":"patch3.zip",
         "size":6382378,
         "checksum":"25efa1e9145a4777deaf589c5b28d9ad"
      },
      "user.cfg":{  
         "name":"user.cfg",
         "type":"file",
         "path":"user.cfg",
         "size":819,
         "checksum":"489a315ac832513f4581ed903ba2886e"
      }
   }
}

I have looked into using GSON and JACKSON but can't figure out how to make them work when pulling out dynamic data from JSON. 我已经研究过使用GSON和JACKSON,但是在从JSON中提取动态数据时无法弄清楚如何使它们工作。

I am using the org.json LIB. 我正在使用org.json LIB。

ReadUrl.java ReadUrl.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadUrl {
    static String getUrl(String urlString) throws Exception {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);
            return buffer.toString();
        } finally {
            if (reader != null)
                reader.close();
        }
    }
}

Main.java Main.java

import org.json.JSONArray;
import org.json.JSONObject;

public class Main {
    private static final boolean isDebugMode = true;
    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception  {
        String sReadURL = ReadUrl.getUrl("http://www.aerosimulations.com/wp-content/uploads/example.json");

        if (isDebugMode) {
            System.out.println(sReadURL);
        }

        JSONObject responseJSON = new JSONObject(sReadURL);
        JSONObject obj1_JSON = responseJSON.getJSONObject("patches");

        JSONArray jPatches = obj1_JSON.names();

        if (isDebugMode) {
            System.out.println(jPatches);
        }

        for(int i = 0; i < jPatches.length(); i++){

        }

    }
}

My requirement is, I need to be able to get the patch names from 'patches' and return a usable java array. 我的要求是,我需要能够从“补丁”中获取补丁名称并返回可用的Java数组。

I would use Jackson Tree Model for this case: 在这种情况下,我将使用Jackson树模型:

    //first, you create a mapper object
    ObjectMapper mapper = new ObjectMapper();

    //then you create a JsonNode instance representing your JSON root structure
    JsonNode root = null;
    try {
        root = mapper.readTree(json);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //here you get the list of your patches nodes
    JsonNode list = root.path("patches");

    //then you can iterate through them and get any inner value
    for (JsonNode patch : list) {
        //for example, you can get a file name
        System.out.println(patch.path("name"));
    }

The output of this code will be: 此代码的输出将是:

patch1.zip
patch2.zip
patch3.zip
user.cfg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM