user26480
user26480

Reputation: 387

JSON parser error with java:

I would like to parse a json file, here is my code:

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:

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.

Upvotes: 0

Views: 12195

Answers (4)

Alex Gidan
Alex Gidan

Reputation: 2679

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.

EDIT

The JSON content you posted is actually correct (good you verified with web utility jsonlint), as it defines an array of elements. The problem rises from the usage of Java API for JSON. Indeed, as shown by @nogard in his answer, the JSONObject String constructor is expecting some JSON content and NOT the file name. (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).

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:

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

Upvotes: 1

Dev
Dev

Reputation: 2325

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. For more info look link.

After modification your json will look like below.

[
    {
        "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.

Upvotes: 2

nogard
nogard

Reputation: 9706

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"):

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

Upvotes: 2

Your json is not a json at all if it doesn't begin with "{"... that is the main interpretation of the Exception message.

Reformat the json doc and try again.

Upvotes: 1

Related Questions