0

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.

1
  • 2
    I have seen your listesanscoord.json and the data it contains are not in json format, a lot of problem is there Commented Nov 19, 2015 at 10:42

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

dropbox.com/s/pxn04jn4ep2gmig/listesanscoord.json?dl=0 I changed my file as you told me, but still problem
yes this file has still some error try to past your json on link you will get which lines contains errors.
oh yes sorry , look now .
your file looks fine ,this json should work fine with java code.
As you can see your json is not a single json Object but instate it is aray of json object you need to create Object of JSONArray try JSONArray jsonArrayObj = new JSONArray(jsonTxt); should resolve your problem.
|
2

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 ); 

1 Comment

Also the file listesanscoord.json is not in JSON
1

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.

Comments

1

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 ); 

2 Comments

look to my file now please it's ok? or not
@user26480 Yes, your file is now correct. The problem consists now in your JSON API usage, so I updated my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.