views:

67

answers:

3

I am new to JSON. Are there any methods in JSON parser to remove the comment characters from a response.

Eg. //{"response":"success".......

its SBJson for iPhone. from http://code.google.com/p/json-framework

+2  A: 

The JSON grammar doesn't allow comments. That doesn't answer your question obviously, but I suspect you'll have to do some string manipulation and replace all those comment characters with empty strings and parse it with the JSON library only after doing so.

Jonathon
A: 

Can you post some more code? What part of the JSON string do you need?

Just don't parse the response into your dictionary/object/whatever...

Canada Dev
Well the whole response is in comment block. Just need to remove the first two characters "//" from the response and use the remaining part for extracting data.
ashokbabuy
A: 

The JSON parsers are very finicky about what is at the start of a JSON block to parse - they DO NOT like characters other than "{" at the start (at least that's what I found with TouchJSON, and it sounds like your case with SBJson is similar).

So just take your string and eliminate any characters before the opening "{", then you can parse:

NSRange startJSONRange = [myJSONString rangeOfString:@"{"];
startJSONRange.length = myJSONString.length - startJSONRange.location;
NSString *correctJSONString = [myJSONString substringWithRange:startJSONRange];

// parse correctJSONString

That will work, but the REAL fix is to tell whoever is sending you JSON to cut out the nonsense and send real JSON.

Kendall Helmstetter Gelner
That worked like a charm.
ashokbabuy