简体   繁体   English

为什么我的JSON-Simple JSONArray给我一个nullpointer异常?

[英]Why does my JSON-Simple JSONArray give me a nullpointer exception?

I'm following this tutuorial here , and my JSON object is near enough the same, except I have this sort of format: 我在这里关注这个tutuorial,我的JSON对象已经足够接近了,除了我有这种格式:

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}

Here is my relevant code: 这是我的相关代码:

Object obj = parser.parse(new FileReader("exampleJson.json"));

JSONObject jsonObject = (JSONObject) obj;
JSONObject user = (JSONObject) jsonObject.get("user");
JSONArray list = (JSONArray) user.get("SomeList");

Then my program goes off an gets the values form the keys etc. Or it would but I get a NullPointerException from eclipse. 然后我的程序关闭从键等获取值等。或者它会,但我从eclipse得到一个NullPointerException Why is this? 为什么是这样?

It should unpackage my .json file into jsonObject , unpackage the "user" key as a JSONObject user , then unpackage the "SomeList" key as a JSONArray called list . 应该解包我.json文件到jsonObject ,解包“用户”键作为一个JSONObject user ,然后解包“SomeList”键作为JSONArray叫list Except when it does so it must be trying to put one of val1 or val2 into a part of the JSONArray that does not exist and just points into the abyss of null . 除非它这样做,否则必须尝试将val1val2一个放入不存在的JSONArray的一部分中,并且只指向null的深渊。 What have I done to deserve this wrong ? 我做 什么 值得这个 错?

How do I fix my program? 我该如何修复我的程序?

Your code is working well for me 您的代码对我来说很有用

public class App {

    public static void main(final String[] args) throws FileNotFoundException, IOException, ParseException {
        final Object obj = new JSONParser().parse(new FileReader("D:/a.json"));
        final JSONObject jsonObject = (JSONObject) obj;
        final JSONObject user = (JSONObject) jsonObject.get("user");
        final JSONArray list = (JSONArray) user.get("SomeList");
        System.out.println(list);
    }
} 

File D:/exampleJson.json 文件D:/exampleJson.json

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}  

output is 输出是

["val1","val2"]

Using the exact same json file: 使用完全相同的json文件:

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}

Here is the code I use (with the imports to really make sure we have the same ones): 这是我使用的代码(导入确实我们有相同的代码):

import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Test {

    public static void main(String[] args) throws Exception {
        Object obj = new JSONParser().parse(new FileReader("t.json"));

        JSONObject jsonObject = (JSONObject) obj;
        JSONObject user = (JSONObject) jsonObject.get("user");
        JSONArray list = (JSONArray) user.get("SomeList");
        System.out.println(list);
    }
}

And here is the output: 这是输出:

["val1","val2"]

I used maven and m2e to import the library. 我使用maven和m2e导入库。 The version used for this test was json-simple-1.1.jar . 用于此测试的版本是json-simple-1.1.jar

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

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