gpt4 book ai didi

java - 错误: non-static method get(Object) cannot be referenced from a static context

转载 作者:行者123 更新时间:2023-12-01 11:00:05 27 4
gpt4 key购买 nike

下面的代码显示:

JavaApplication1.java:34: error: non-static method get(Object) cannot be referenced from a static contextJSONArray cars = (JSONArray) JSONObject.get("cars");                                                      
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JavaApplication1 {

@SuppressWarnings("unchecked")
public static void main(String[] args) {

JSONParser parser = new JSONParser();

try {
JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Glambert/Dropbox/java/New folder/perfection/UPdate/json.txt"));

for (Object o : a)
{
JSONObject person = (JSONObject) o;

String name = (String) person.get("name");
System.out.println(name);

String city = (String) person.get("city");
System.out.println(city);

String job = (String) person.get("job");
System.out.println(job);

JSONArray cars = (JSONArray) JSONObject.get("cars");

for (Object c : cars)
{
System.out.println(c+"");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}

有人知道为什么会出现这种情况吗?

(顺便说一句,这段代码是在网上找到的,我对其进行了编辑以测试运行,以便我可以创建一个新代码来接收不同类型的txt文件。)

项目:来自 StackOverflow 页面的代码 How to read json file into java with simple JSON library

代码作者:https://stackoverflow.com/users/1212960/greg-kopff

最佳答案

检查这一行

 JSONArray cars = (JSONArray) JSONObject.get("cars");

更改它

 JSONArray cars = (JSONArray) person.get("cars");

问题是因为您直接在类上调用 get 方法。

关于java - 错误: non-static method get(Object) cannot be referenced from a static context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33414535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com