YashMittal05 / apex-library

Automatically exported from code.google.com/p/apex-library
0 stars 0 forks source link

Expected a , or } #4

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi

This parser works fine with Google APIs. I am trying to use elance API to 
search for the jobs and it throws me an exception "Expected a , or }". The JSON 
response of elance and Google looks different.

Would I have to modify this class so that it work with elance API?

Thanks

Original issue reported on code.google.com by geekyos...@gmail.com on 18 Apr 2011 at 7:23

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
This is issue is present when you have double-quotes within the json object:

For example:

 String body = '{"name":"Hi Ron is \\"this\\" working? "}';

 JSONObject jObj = new JSONObject( new JSONObject.JSONTokener( body ) );
 system.assertNotEquals(jObj, null);

Original comment by aldo...@gmail.com on 7 Sep 2011 at 7:04

GoogleCodeExporter commented 8 years ago
I fixed the issue adding the following method to JSONObject.JSONTokener class

/*
     * Sanitize Json input -- Aldo Fernandez @aldoforce
     *
     * @return sanitized string
     */
    public String sanitize(String s) {
        String result;
        try {
            result = s.replace('\\"', '\'');
        }
        catch (Exception ex) {
            result = '';        
        }
        return result; 
    }

Then modify the JSONObject.JSONTokener constructor to use the method described 
above:

Look in the line #996 and modify:

     this.reader = s;

with

     this.reader = this.sanitize(s);

Regards,
--Aldo 
@aldoforce

Original comment by aldo...@gmail.com on 7 Sep 2011 at 7:57