【问题标题】:JSONArray to usable Java/String arrayJSONArray 到可用的 Java/String 数组
【发布时间】:2017-12-14 04:13:32
【问题描述】:

这不是重复的

我的 JSON 不在 JSONArray 中,我的 JSON 是所有对象。但是,我已将对象传递到 JSONArray 中,因为这是我弄清楚如何在“补丁”中获取每个对象的唯一方法。

现在我已将所有内容都转换为 JSONArray,但我无法按照我的意愿使用它。这样做的最终目标是从服务器端获取 JSON(顺便说一下,这是动态的,补丁下的补丁文件会重新生成并添加新补丁)最终我想获取该数组列表并使用它来搜索那些用户目录中的包,但那是以后的。

现在我不知道如何从 JSONArray 中转换数据。

JSON 目前在服务器端的结构如下。 (这个结构不能改变)。

{  
   "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"
      }
   }
}

我研究过使用 GSON 和 JACKSON,但在从 JSON 中提取动态数据时不知道如何使它们工作。

我正在使用 org.json LIB。

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

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++){

        }

    }
}

我的要求是,我需要能够从 'patches' 中获取补丁名称并返回一个可用的 java 数组。

【问题讨论】:

  • 没有重复,我在问这个问题之前对如何做到这一点进行了广泛的研究。我们的 JSON 设置为两种不同的结构,我无法使用 getString。 @vinS

标签: java json


【解决方案1】:

我会在这种情况下使用杰克逊树模型:

    //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"));
    }

这段代码的输出将是:

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

【讨论】:

  • 你一字一句地解决了两个问题。我现在了解如何使用jacksons(据我了解,这是一种更好的方法)。我不能感谢你。现在我可以使用它来实际对每个补丁文件进行我需要的操作。这真是太棒了。我可能还可以创建一个字符串数组并将每个读取行添加到这些数组中。这将是完美的。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-10-05
  • 2023-03-22
  • 1970-01-01
  • 2016-01-14
  • 2013-11-10
  • 2017-03-28
  • 1970-01-01
  • 2016-08-27
相关资源
最近更新 更多