gpt4 book ai didi

java - JSON解析器: Unexpected token COLON(:)

转载 作者:行者123 更新时间:2023-12-01 13:05:18 24 4
gpt4 key购买 nike

我正在尝试使用 JSONParse 解析 json 文件并收到此错误,该错误发生在以下 json 的开头:

位置 11 处出现意外标记 COLON(:)。

{"276716878": {
"followers": [
2435018580,
1664252310,
372262434
],
"following": [
16211434,
945440959,
130682467,
264257750,
900526363,
318231688,
40335029,
64044676
]
}}

我还使用以下内容编写了 json 文件:

FileWriter out = new FileWriter(JSON_FILE);
out.write(json.toString(1));
out.flush();
out.close();

最佳答案

您传递的 json 字符串中可能存在一些格式错误。 Validate your json string 。下面的示例代码工作正常。

import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

public class Test {

private static final Logger logger = Logger.getLogger(Test.class.getName());

private static final String JSON_FILE = "/home/visruth/Desktop/Visruth.txt";

public static void main(String[] args) {

String jsonText = "{\"215876567\": { \"followers\": [ 2464054938, 772677937]}}";

try (

FileWriter out = new FileWriter(JSON_FILE);

) {

JSONObject json = new JSONObject(jsonText);

int indentFactor = 1;
String prettyprintedJSON = json.toString(indentFactor);
System.out.println(prettyprintedJSON);
out.write(prettyprintedJSON);

} catch (JSONException e) {
logger.log(Level.SEVERE, e.getMessage());
} catch (IOException e) {
logger.severe(e.getMessage());
}
}
}

jsonText 变量中分配您的 json 文本并尝试。

关于java - JSON解析器: Unexpected token COLON(:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307328/

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