【发布时间】:2016-01-29 14:42:16
【问题描述】:
如下代码所示:
JavaApplication1.java:34:错误:不能从静态上下文引用非静态方法 get(Object)
JSONArray 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
【问题讨论】:
-
如果代码是在网上找到的,你应该注明原作者并链接到你找到它的地方。
-
@RealSkeptic 感谢您的通知!贷记!
标签: java json-simple