简体   繁体   English

使用 json-simple 解析 Java 中的 JSON 文件

[英]Parsing a JSON file in Java using json-simple

I have created a.json file:我创建了一个 .json 文件:

{
  "numbers": [
    {
      "natural": "10",
      "integer": "-1",
      "real": "3.14159265",
      "complex": {
        "real": 10,
        "imaginary": 2
      },
      "EOF": "yes"
    }
  ]
}

and I want to parse it using Json Simple, in order to extract the content of the "natural" and the "imaginary".我想用 Json Simple 来解析它,以便提取“自然”和“想象”的内容。

This is what I have written so far:这是我到目前为止所写的:

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("...")); //the location of the file
JSONObject jsonObject = (JSONObject) obj;
String natural = (String) jsonObject.get("natural");
System.out.println(natural);

The problem is that the value of natural is "null" and not "10".问题是 natural 的值是“null”而不是“10”。 Same thing happens when I write jsonObject.get("imaginary").当我写 jsonObject.get("imaginary") 时也会发生同样的事情。

I have looked at many websites (including StackOverflow), I have followed the same way most people have written, but I am unable to fix this problem.我查看了许多网站(包括 StackOverflow),我遵循大多数人编写的相同方式,但我无法解决这个问题。

You need to find the JSONObject in the array first. 您需要首先在数组中找到JSONObject You are trying to find the field natural of the top-level JSONObject , which only contains the field numbers so it is returning null because it can't find natural . 您正在尝试查找顶级JSONObject natural字段,该字段仅包含字段numbers因此它返回null因为它无法找到natural

To fix this you must first get the numbers array. 要解决此问题,您必须先获取数字数组。

Try this instead: 试试这个:

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("...")); //the location of the file
JSONObject jsonObject = (JSONObject) obj;
JSONArray numbers = (JSONArray) jsonObject.get("numbers");

for (Object number : numbers) {
    JSONObject jsonNumber = (JSONObject) number;
    String natural = (String) jsonNumber.get("natural");
    System.out.println(natural);
}

The object in your file has exactly one property, named numbers . 文件中的对象只有一个属性,名为numbers
There is no natural property. 没有natural财产。

You probably want to examine the objects inside that array. 您可能想要检查该数组中的对象。

Adding on to @Jermey Hanion's answer and comment, here is what I did to get "imaginary" and "natural"加上@Jermey Hanion 的回答和评论,这是我为获得“虚构”和“自然”所做的事情

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("...")); //the location of the file
JSONObject jsonObject = (JSONObject) obj;
JSONArray numbers = (JSONArray) jsonObject.get("numbers");

for (Object number : numbers) {
    JSONObject jsonNumber = (JSONObject) number;
    String natural = (String) jsonNumber.get("natural");
    JSONObject complex = (JSONObject) jsonNumber.get("complex");
    String imaginary = (String) complex.get("imaginary");
    System.out.println(natural);
}

Jeremey's answer for getting imaginary is not correct, or maybe it was correct. Jeremey 关于获得想象的答案是不正确的,或者它可能正确的。 The above snippet is to my knowledge working on my project.据我所知,上面的代码片段是在我的项目上工作的。

PS. PS。 Sorry for reviving the thread but I thought it would be a useful resource for people looking to learn JSON.simple很抱歉恢复线程,但我认为这对于希望学习 JSON.simple 的人来说是一个有用的资源

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

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