2
String json = "{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}";

try {
    JSONObject jsonObject = JSONObject.fromObject(json);
    String name = jsonObject.getString("name");
    String address = jsonObject.getString("address");
    System.out.println("name is:" + name);
    System.out.println("address is:" + address);
    JSONArray jsonArray = jsonObject.getJSONArray("array");
    for (int i = 0; i < jsonArray.size(); i++) {
        System.out.println("item " + i + " :" + jsonArray.getString(i));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Eveything is OK. But when I put {'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'} into a file.

        File file = new File(fileName);  
        BufferedReader reader = null;           
        try {  
            reader = new BufferedReader(new FileReader(file));  
            String tempString = null;  
            while ((tempString = reader.readLine()) != null){                   
                JSONObject jo = JSONObject.fromObject(tempString.trim());
                String id = jo.getString("id");
                String name = jo.getString("name");
                log.info(id + ":" + name);
            }  
            reader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 

It tells me Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of "{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}. What's the problem in this code? Can any figure it out for me? Thanks.

my file:

{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}

9
  • 1
    don't suppose there's an empty line in the file or something like that? Maybe you just need to trim the string: JSONObject.fromObject(tempString.trim()) Commented Jul 24, 2014 at 6:42
  • no empty line there. I did not write the whole error sentence, and i've modified it.trim() does not work. Commented Jul 24, 2014 at 6:47
  • Post you file content. How json is written in file. Commented Jul 24, 2014 at 7:15
  • 2
    Perform a hex dump of the file. Check there isn't a BOM - e.g. the file might start with the bytes EF BB BF. It may not matter in this case but you should use a Charset to specify an encoding to your Reader. Commented Jul 24, 2014 at 7:20
  • where is this id parameter in your JSON. You are trying jo.getString("id") ? Commented Jul 24, 2014 at 7:31

3 Answers 3

2

From the comment:

I use

InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
reader = new BufferedReader(isr);
String tempString = null;
while ((tempString = reader.readLine()) != null){

, and my file starts with EE BB BF, I've checked.

The issue is that the file starts with a BOM. The JSON decoder expects the file to begin with a character that can start a JSON type but it's getting U+FEFF.

It would be best if the JSON file did not start with a BOM.

If you must handle this case then you can do it with the buffer:

BufferedReader buf = new BufferedReader(isr);
// remove BOM
buf.mark(1);
if(buf.read() != '\uFEFF') {
  buf.reset();
}
// continue...
Sign up to request clarification or add additional context in comments.

1 Comment

buf.mark.error --- The method mark(int) in the type BufferedReader is not applicable for the arguments (). I try buf.mark(0); it works. Thanks you very much!!!
0

If you look at the string returned in the error, " {'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}", you'll see that in character 1 there is actually a space. What happens if you try removing this space from your file?

1 Comment

Actually there is no space there. Sorry it's my mistake.
0

You have to replace your single quotes ' with double quotes ".

{
    "name": "Tom",
    "array": [
        {
            "a": "111",
            "b": "222",
            "c": "333"
        },
        {

        },
        {
            "a": "999"
        }
    ],
    "address": "York"
}

As defined in the JSON specification http://www.ietf.org/rfc/rfc4627.txt

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.

And quotation mark means "

1 Comment

@Ron They are not the same for json. You can test it here jsonlint.com Test it with single and double quotes. Single quotes will not be valid.

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.