json.simple无法将字符串强制转换为JSONObject错误



我试图使用JSON.simple java库从JSON文件中读取,但每次运行以下代码时:

JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader("plugins/LogicGates/ands.json"));
Iterator i = jsonArray.iterator();
while (i.hasNext()) {
JSONObject obj = (JSONObject) i.next();
Block gate = (Block) obj.get("Gate");
Block inp1 = (Block) obj.get("Inp1");
Block inp2 = (Block) obj.get("Inp2");
ands.add(new And(gate, inp1, inp2));
}

它返回:

java.lang.ClassCastException: class java.lang.String cannot be cast to class org.json.simple.JSONObject (java.lang.String is in module java.base of loader 'bootstrap'; org.json.simple.JSONObject is in unnamed module of loader 'app')

有人知道为什么吗?提前感谢您的回复。

编辑:

因此,我以某种方式使旧代码正常工作,下面是它现在的样子:

JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader("plugins/LogicGates/ands.json"));
Iterator i = jsonArray.iterator();
while (i.hasNext()) {
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse((String) i.next());
Block gate = (Block) obj.get("Gate");
Block inp1 = (Block) obj.get("Inp1");
Block inp2 = (Block) obj.get("Inp2");
ands.add(new And(gate, inp1, inp2));
}

但现在我得到了一个完全不同的错误:

Unexpected character (C) at position 8.

我被告知我的JSON无效,所以下面是编写JSON:的代码

FileWriter writer = new FileWriter("plugins/LogicGates/ands.json");
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
for(And a : ands) {
obj.put("Gate", a.getGate());
obj.put("Inp1", a.getInp1());
obj.put("Inp2", a.getInp2());
jsonArray.add(obj.toJSONString());
}
writer.write(jsonArray.toJSONString());
writer.close();

我在您更新文章之前写了这个例子。

我以为你可能在用GSON。。。但您没有告诉我们您使用的是哪个库,也没有给出文件格式的示例。

无论如何,我希望这个SSCCE有帮助:

test.json

{
"books": [
{"title":"The Shining", "author":"Stephen King"},
{"title":"Ringworld","author":"Larry Niven"},
{"title":"The Moon is a Harsh Mistress","author":"Robert Heinlein"}
]
}

HelloGson.java

package com.example.hellogson;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Illustrates one way to use GSON library to parse a JSON file
* 
* EXAMPLE OUTPUT:
*   Author: Stephen King, title: The Shining
*   Author: Larry Niven, title: Ringworld
*   Author: Robert Heinlein, title: The Moon is a Harsh Mistress
*   Done parsing test.json
*/
public class HelloGson {
public static final String FILENAME = "test.json";
public static void main(String[] args ) {
try {
// Open file 
FileReader reader = new FileReader(FILENAME);
// Parse as JSON.  
// Typically, the toplevel element will be an "object" (e.g."books[]" array
JsonParser jsonParser = new JsonParser();
JsonElement rootElement = jsonParser.parse(reader);
if (!rootElement.isJsonObject()) {
throw new Exception("Root element is not a JSON object!");
}
// OK: Get "books".  Let's assume it's an array - we'll get an exception if it isn't.
JsonArray books = ((JsonObject)rootElement).get("books").getAsJsonArray();
for (JsonElement book : books) {
// Let's also assume each element has an "author" and a "title"
JsonObject objBook = (JsonObject)book;
String title = objBook.get("title").getAsString();
String author = objBook.get("author").getAsString();
System.out.println("Author: " + author + ", title: " + title);
}
System.out.println("Done parsing " + FILENAME);
} catch (Exception e) {
System.out.println("EXCEPTION: " + e.getMessage());
e.printStackTrace();
}
}
}

注意:

  • 也许比映射单个JSON元素更好的方法是将整个JSON数据结构映射到Java对象。GSON对此表示支持;Jackson是另一个常用的Java库。

  • 将来,请考虑写一个"小的自包含的例子"(像上面这样的SSCCE。这是一个更快地得到更好、更准确答案的好方法。

  • 最后,如果JSON格式不正确,NO库将为您提供帮助。有许多在线验证检查器。例如:https://jsonlint.com/

'希望现在和将来都能有所帮助。

最新更新