1

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.

iaindownie
  • 1,046
  • 12
  • 28
  • 1
    That's not a valid JSON. `success` looks like an array of strings but strings are supposed to be surrounded by quotes. – Bhesh Gurung Dec 28 '11 at 15:04
  • If I put my 'good' JSON string into http://jsonlint.com/ it comes back as valid. – iaindownie Dec 28 '11 at 15:14
  • Just because it's syntactically valid it doesn't mean it does what you want. Oh, how much easier life would be then (or possibly not). – gustafc Dec 28 '11 at 15:16

1 Answers1

4

Can anyone suggest how to properly escape the comma within the placename string, either in the Java when creating the JSON [...] ?

Your problem is that you're return many values as a single string - you've got the double quotes outside the brackets, rather than inside (enclosing the individual values). You probably want to usa a JSON list of strings instead of chopping stuff up yourself:

{"success": ["TG2301_Stoke Holy Cross", 
             "TF7439_Thornham Corner", 
             "TL8583_Thetford, North"]}

This way, the commas within quotes aren't magical in any way, and you get your place names as nice strings.

Having said that, what you actually want to do is return a JSON object (which works much like a Java dictionary) instead of a list of strings, so that you even get your key/value pairs split up nicely:

{"success": {"TG2301": "Stoke Holy Cross", 
             "TF7439": "Thornham Corner", 
             "TL8583": "Thetford, North"}}
gustafc
  • 28,465
  • 7
  • 73
  • 99
  • OK, I'm sure I can create this, but what StringTokenizer 'tokens' would you use to parse this? Comma would surely have the same issues? – iaindownie Dec 28 '11 at 15:16
  • Forget last comment - I can see you are suggesting JSON objects instead of a single string. Thanks. – iaindownie Dec 28 '11 at 15:20
  • Regarding the approach of placing the double quotes inside the brackets - what if part of the string itself contains double quotes? – BornToCode Nov 03 '14 at 09:29
  • 1
    @BornToCode you escape them, just like in Java or JavaScript (or any C-derived language): `"this string contains \"double quotes\""` – gustafc Nov 03 '14 at 12:29