简体   繁体   English

使用 org.json 解析 Java 中 JSONArray 中的内部 JSON 文件

[英]Parsing internal JSON file in JSONArray in Java using org.json

I would like to be able to parse a JSON file that is structured something like:我希望能够解析结构如下的 JSON 文件:

[
  {
    "id": 1,
    "name": {
      "first": "name",
      "secound": "name",
      "last": "name"
    },
    "skills": [
      "python",
      "javascript"
    ],
    "otherInfo": {
      "something": 45,
      "something2": 49
    }
  },
  {
    "id": 2,
    "name": {
      "first": "name",
      "secound": "name",
      "last": "name"
    },
    "skills": [
      "python",
      "javascript"
    ],
    "otherInfo": {
      "something": 45,
      "something2": 49
    }
  }
etc...
]

into something looking like变成看起来像的东西

array(
    [0] => array(
                   0 => "id"
                   1 => "firstname"
                   2 => "otherData"
                ),
    [1] => array(
                   0 => "id"
                   1 => "firstname"
                   2 => "otherData"
                ),
etc...
)

I'm pretty sure i have an idea on how to convert it into the format I want, but im having trouble actually reading the data from the file.我很确定我对如何将其转换为我想要的格式有一个想法,但是我在实际从文件中读取数据时遇到了麻烦。

The two major issues im having:我有两个主要问题:

  • Reading it from inside the jar.从 jar 内部读取它。
  • Most of the examples used json.simple, and the json library i'm using doesn't seem to have that.大多数示例使用 json.simple,而我使用的 json 库似乎没有。

I tried some examples online, and a couple of the answers on this post but no luck the biggest issue is that all of them are giving examples for reading an external file, while I'm trying to read one that is packaged inside the jar.我在网上尝试了一些示例,并且在这篇文章中给出了一些答案,但没有运气,最大的问题是所有这些都提供了读取外部文件的示例,而我正在尝试读取 jar 中打包的示例。

My project tree:我的项目树:

MyProject
      L src
          L myPackage
                  L MyClass.java
                  L MyJsonFile.json

The closest thing that I'm guessing almost worked is this (from the link above):我猜最接近的事情是这个(来自上面的链接):

import org.json.JSONArray;

//code

JSONArray myJSONArray = new JSONArray(Main.class.getResourceAsStream("myFile.json"));

But that only seems to throw an error: org.json.JSONException: JSONArray initial value should be a string or collection or array.但这似乎只会引发错误: org.json.JSONException: JSONArray initial value should be a string or collection or array.

Thanks in advance!提前致谢!

The problem is org.json.JSONArray will only accept String , Collection or Array but you are trying to pass the InputStream object.问题是org.json.JSONArray将只接受StringCollectionArray但您正在尝试传递InputStream object。 which is what error messages also says这也是错误消息所说的

org.json.JSONException: JSONArray initial value should be a string or collection or array org.json.JSONException: JSONArray 初始值应该是字符串或集合或数组

So first convert the InputStream into String所以首先将InputStream转换为String

InputStream  inputStream = Main.class.getResourceAsStream("myFile.json");

InputStreamReader isReader = new InputStreamReader(inputStream);

BufferedReader reader = new BufferedReader(isReader);
  StringBuffer sb = new StringBuffer();
  String str;
  while((str = reader.readLine())!= null){
     sb.append(str);
  }

And then convert it into JSONArray然后将其转换为JSONArray

JSONArray myJSONArray = new JSONArray(sb.toString());

After here you can iterate the JSONArray using for loop, like here在这里之后,您可以使用 for 循环迭代JSONArray ,就像这里

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

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