Hi All
I'm facing issue while reading the json string with escape double quote in it which need to be processed by GSON after the extraction. GSON failing with malformed exception as the string conversion not unescaping the \.
File looks like this (first column 12345 is the key separated by tab with the second column json string):
12345 {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> <OldPositions start=\\\"0\\\" end=\\\"10\\\" /> </saleinfo>\"}}}
I would be getting the record handler in byte array instead of actual file which i converted to string and then split the record into string array with first element of array as key (12345) and second as Json string. But the json string loads with the escape character.
Code below:
byte[] entryData = "file content here in byte array"; String str = new String(entryData); //converting byte array to string String[] lines = str.split("[\\r\\n]+"); //split into records for(int i=0; i< lines.length; i++) { String[] strArray = lines[i].split("\\t"); String key = strArray[0]; String jsonStr = new String(strArray[1]); System.out.println("JsonString --- "+jsonStr); //output is {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> <OldPositions start=\\\"0\\\" end=\\\"10\\\" /> </saleinfo>\"}}} //backslash not escaping }
Following the above, i would be parsing the jsonStr using GSON (cannot use json parser due to requirement) and extract the tag and its' values. As you see each element is JsonObject..
I got lost on why the unescape not happening converting to string and i cannot use replace due to single and three backslashes in the jsonStr.
Could you please help me in resolving the issue and let me know if you need more info than provided above.
Thanks
kmove