When my web page choked, I wasn't too surprised. I'm never surprised when code I write has bugs initially. I added a log statement to show the data being passed in and was surprised to see that it looked like this:
{"a": "{b={c=d}}", "e":"f"}This was not correct. This should have looked like this:
{"a": "{"b":"{"c":"d"}"}", "e":"f"}To create the JSONObject that I was passing in, I passed it in a HashMap whose keys were all strings but whose values were either strings or another HashMap. So to create the above structure there would be code like this:
HashMap<String,Object> top = new HashMap<String,Object>(); HashMap<String,Object> a = new HashMap<String,Object>(); HashMap<String,Object> b = new HashMap<String,Object>(); b.put("c", "d"); a.put("b", b); a.put("e", "f"); top.put("a",a);It seemed that the JSONObject code was flattening out my objects and producing incorrect JSON (WTF equal signs!) as a result. I put together a quick workaround to recursively replace any HashMap values with JSONObjects like this:
JSONObject jsonIfy(HashMap<String,Object> map){ HashMap<String,Object> fixed = new HashMap<String,Object>(); for (String key : map.keySet()){ Object value = map.get(key); if (value instanceof Map){ value = jsonIfy((HashMap<String,Object>) value); } fixed.put(key,value); } return new JSONObject(fixed); }This worked perfectly.
Is this a bug, or does that 'put' statement call toString on its HashMap argument rather than converting to a JSONObject....
ReplyDeleteDefinitely a bug, since on newer versions of Android (notably Lollipop) creating Maps with plain Object as value type works as it should.
ReplyDeleteSpectrum internet plans allow customers to get the best discount to get the same quality at a low price. Usually, spectrum internet plans come with the lowest price and offer the best services which include no data cap, high speed, and fast downloading and uploading speed.
ReplyDelete