I have a problem reading in strings with comma's within a JSON object within my Android project. A working JSON string is below:
{"success": "[TG2301_Stoke Holy Cross, TF7439_Thornham Corner, TL8583_Thetford]"}
But sometimes the place names have comma's, and that throws a wobbly with the JSON and StringTokenizer methods that I use to parse the JSON into key:values pairs, as shown below in last entry:
{"success": "[TG2301_Stoke Holy Cross, TF7439_Thornham Corner, TL8583_Thetford, North]"}
Before I found this bug I was using the following to parse the JSON:
StringTokenizer str1 = new StringTokenizer(str,"[,]");
while (str1.hasMoreTokens()) {
String val = str1.nextToken().trim();
// Split the string at the underscore and do some stuff
}
Can anyone suggest how to properly escape the comma within the placename string, either in the Java when creating the JSON, or within the StringTokenizer? Thanks.