JSON: net.sf.json.JSONException: Expected a ',' or '}' at character net.sf.json.JSONException

2 respostas
Michel_M

Quando eu tendo dar um "JSONObject.fromObject" esta estourando uma exceção aqui 'true' = 'true' pq a aspas simples estão dentro de outra aspas simples, alguem sabe algum tipo de caracter de escape para essa 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(clearForJSON(json), cfg);// estoura a exceção
			System.out.println(jsonObjeto.toString());
		} catch (final JSONException e) {

			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	public static String clearForJSON(String evaluate) {
		if (evaluate == null) {
			return null;
		}
		evaluate.replaceAll("\r\n", " ");
		evaluate = evaluate.replaceAll("\n", " ");
		evaluate = evaluate.replaceAll("\r", " ");
		evaluate = evaluate.replaceAll("\f", " ");
		//evaluate = evaluate.replaceAll("'", "\'");
		
		return evaluate;
	}

Exceção que estoura:

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)

Desde já agradeço a todos

2 Respostas

romarcio

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

Michel_M

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

Cara desculpa pela a demora em responder…

Eu já tinha testando esse caracter de escape… mais msm assim agradeço!

Criado 8 de janeiro de 2013
Ultima resposta 9 de jan. de 2013
Respostas 2
Participantes 2