简体   繁体   English

Java的JSON解析器错误:

[英]JSON parser error with java:

I would like to parse a json file, here is my code: 我想解析一个json文件,这是我的代码:

import org.json.JSONArray;
import org.json.JSONObject;

public class principale {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fichier ="C:\\listesanscoord.json";
        JSONObject obj = new JSONObject("fichier");
        String pageName = obj.getJSONObject("pageInfo").getString("pageName");

        JSONArray arr = obj.getJSONArray("oaci");
        for (int i = 0; i < arr.length(); i++)
        {
            String url = arr.getJSONObject(i).getString("url");

        }
    }

}

and here is my json file: listesanscoord.json I have the following error: 这是我的json文件: listesanscoord.json我有以下错误:

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    at org.json.JSONObject.<init>(JSONObject.java:198)
    at org.json.JSONObject.<init>(JSONObject.java:325)
    at metar.principale.main(principale.java:13)

Can someone help me please I am unable to find where is the problem, thank you. 有人可以帮我吗,我找不到问题所在,谢谢。

The problem is in these lines: 问题出在以下几行:

String fichier ="C:\\listesanscoord.json";
JSONObject obj = new JSONObject("fichier");

You should normally pass content of the file, not just it's name (or "fichier"): 通常,您应该传递文件的内容,而不仅仅是文件名(或“ fichier”):

InputStream is = JsonParsing.class.getResourceAsStream("C:\\listesanscoord.json");
String jsonTxt = IOUtils.toString( is );
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt ); 

In Extension to @nogard answer I detected that there is error in JSON text in your file ,JSON string are like java map or javascript object have key value pairs,in your file key is define wrongly, it should be in double quotes (" ") so key value pair will look like "key":"value String" or "key":value Number . @nogard答案的扩展名中,我检测到文件中的JSON文本有错误 ,JSON字符串就像java mapjavascript对象具有键值对,在您的文件键中定义错误,它应该用双引号(“” ),因此键值对看起来就像“ key”:“ value String”“ key”:value Number一样 For more info look link . 有关更多信息,请参见链接

After modification your json will look like below. 修改后,您的json将如下所示。

[
    {
        "oaci": "LFXA",
        "aeroport": "Aérodrome d'Ambérieu",
        "url": "https://fr.wikipedia.org/wiki/A%C3%A9rodrome_d%27Amb%C3%A9rieu",
        "commune": "Chateau-Gaillard, Ambronay"
    }
    //more json objects 
]

if you modify your json file like above will resolve your problem. 如果您像上面那样修改json文件,将解决您的问题。

Your json is not a json at all if it doesn't begin with "{" ... that is the main interpretation of the Exception message. 如果您的json并非以“ {”开头这是Exception消息的主要解释),则根本不是json。

Reformat the json doc and try again. 重新格式化json文档,然后重试。

The exception is pretty clear (for once!). 异常非常明显(一次!)。 The JSON content must start with { or [ , as it must define a root object or an array at least. JSON内容必须以{[开头,因为它必须至少定义一个根对象或数组。

EDIT 编辑

The JSON content you posted is actually correct (good you verified with web utility jsonlint ), as it defines an array of elements. 您发布的JSON内容实际上是正确的(可以通过Web实用程序jsonlint进行验证),因为它定义了元素数组。 The problem rises from the usage of Java API for JSON. 问题是由Java API用于JSON引起的。 Indeed, as shown by @nogard in his answer, the JSONObject String constructor is expecting some JSON content and NOT the file name. 确实,如@nogard在他的答案中所示,JSONObject String构造函数期望一些JSON内容而不是文件名。 (see official doc ). (请参阅官方文档 )。 So your parser is trying to interpret the filename as some JSON content, thus failing (because the filename " fichier " is NOT a valid JSON string). 因此,您的解析器尝试将文件名解释为某些JSON内容,从而失败(因为文件名“ fichier不是有效的JSON字符串)。

So you should grab the file contents before, via an inputstream for instance, and build your JSON object with the help of a serializing utility: 因此,您应该在例如通过输入流获取文件内容之前,并借助序列化实用程序来构建JSON对象:

String json = IOUtils.toString(JsonParsing.class.getResourceAsStream("C:\\listesanscoord.json"));
JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON( json ); 

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

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