Michel Miola
Michel Miola

Reputation: 3

Escape for net.sf.json.JSONObject

When I have to give a JSONObject.fromObject this popping an exception here 'true' = 'true' pq are the single quote inside another single quote, someone knows some kind of escape character for this API?

public static void main(String[] args) {

        String json = "{gA:[ {c:{f:'C#',o:'=',v1:' ('true' = 'true' ) ' }}]}";


        final Map<String, Object> map = new HashMap<String, Object>();



        try {

            JsonConfig cfg = new JsonConfig();
            cfg.setRootClass(LinkedHashMap.class);
            cfg.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
            cfg.setHandleJettisonSingleElementArray(false);
            JSONObject jsonObjeto = JSONObject.fromObject(json, cfg);
            System.out.println(jsonObjeto.toString());
        } catch (final JSONException e) {

            e.printStackTrace();
        }


    }

Exception:

net.sf.json.JSONException: Expected a ',' or '}' at character net.sf.json.JSONException: Expected a ',' or '}' at character 31 of {gA:[ {c:{f:'C#',o:'=',v1:' ('true' = 'true' ) ' }}]}  
    at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)  
    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1099)  
    at net.sf.json.JSONObject.fromObject(JSONObject.java:159)  
    at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:348)  
    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1008)  
    at net.sf.json.JSONObject.fromObject(JSONObject.java:159)  
    at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:348)  
    at net.sf.json.JSONArray._fromJSONTokener(JSONArray.java:1131)  
    at net.sf.json.JSONArray.fromObject(JSONArray.java:125)  
    at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:351)  
    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1008)  
    at net.sf.json.JSONObject._fromString(JSONObject.java:1201)  
    at net.sf.json.JSONObject.fromObject(JSONObject.java:165)  
    at br.com.michel.json.JsonTest.main(JsonTest.java:28)

The output of the json that I desire:

{"ga": [{"c": {"f", "C #", "o": "=", "v1": "('true' = 'true')"}}]}

Upvotes: 0

Views: 4118

Answers (2)

Michel Miola
Michel Miola

Reputation: 3

Dude more when I'm putting this Json:

{gA:[{c:{f:'FIXO',o:'=',v1:'T'}},{gO:[{c:{f:'ID_USUARIO',o:'=',vc:'200'}},{c:{f:'ID_PERFIL',o:'=',vc:'28'}},{c:{f:'C#',o:'=',v1:'(\'true\' = \'true\')'}}]}]}

Overflows this exception:

net.sf.json.JSONException: Expected a ',' or '}' at character 131 of {gA:[{c:{f:'FIXO',o:'=',v1:'T'}},{gO:[{c:{f:'ID_USUARIO',o:'=',vc:'1'}},{c:{f:'ID_PERFIL',o:'=',vc:'1'}},{c:{f:'C#',o:'=',v1:'(\'true\' = \'true\')'}}]}]}

Upvotes: 0

stevevls
stevevls

Reputation: 10843

You can escape the single quotes with backslashes (which themselves have to be escaped since we're working with a Java string literal):

String json = "{gA:[ {c:{f:'C#',o:'=',v1:' (\\'true\\' = \\'true\\' ) ' }}]}";

Running the code with the modified json variable produces this output:

{"gA":[{"c":{"f":"C#","o":"=","v1":" ('true' = 'true' ) "}}]}

This string differs from your desired output in that it has the element v1 instead of u and the value isn't trimmed. I'm not sure if that's a copy/paste typeo or not, though. If that's what you're after, it's simple enough to modify your parsed JSONObject.

Upvotes: 1

Related Questions