1

I am trying to decode a json string with either jsonkit or nsjsonserialization from a server but I am getting the colon : marks changed to equal = signs and commas , changed to semi-colons ; e.g. from server:

"response": { "status": "OK","message": "","timestamp": "30 Mar 2013 11:33:08", 
"url":"/abc/api/getconfig/dev-game?language=en" }...

but the response I get doing either:

JSONDecoder *decodedData = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionStrict];
NSDictionary *parsedList = [decodedData objectWithData:responseData];

or:

NSError *jsonError = nil;
NSDictionary *parsedList = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&jsonError];

gives me:

response =     {
    message = "";
    status = OK;
    timestamp = "30 Mar 2013 11:33:08";
    url = "/abc/api/getconfig/dev-game?language=en";
};
result =     {
    meta =         {
        dateLastModified = "29 Mar 2013 11:59:17";
    };

which gives me a lot of problems while trying to parse. I can do:

NSSTring *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

or put the request url in a browser or jsonlint.com and I get the proper formatting, but I need this in a dictionary, because I need to be able to parse it. Unless it will be better to put this into a different data type, because I will be trying to find specific keys i.e. get every "column" if I have column_1, column_2... Thanks in advance.

meth
  • 1,887
  • 2
  • 18
  • 33
PirateDave
  • 163
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/15180036/putting-json-into-an-array/15181743#15181743 – LE SANG Mar 30 '13 at 01:25
  • @aăâ thanks for at least responding but if you read my question that's one of the ways I'm handling the response. The problem I'm having relates to the encoding after I receive it not if it's an array or dictionary in the response. – PirateDave Mar 30 '13 at 02:52
  • "response": {} this struct tell your response must be a dictionary – LE SANG Mar 30 '13 at 02:54
  • Right, but after putting the response into a dictionary using jsonkit or nsjsonserialization my data has semi-colons ; and equal = signs in it which make it invalid json like the example I gave above – PirateDave Mar 30 '13 at 03:03
  • See my answer. Follow the JSON struct. – LE SANG Mar 30 '13 at 03:12
  • Perfectly normal, as fumoboy states. NSLog (or, more accurately, `description`) formatting for NSArray/NSDictionary predates the adoption of JSON, and is not intended to mimic JSON format. The fact that it dos as well as it does is something of an accident. In the NSLog output you can tell if something is an array or dictionary by the enclosing "bracket characters" -- arrays are "()" (whereas JSON used "[]") and dictionaries (JSON "objects") are "{}" (the same as JSON). – Hot Licks Apr 01 '13 at 11:36

1 Answers1

1

JSONKit and NSJSONSerialization will parse the JSON string for you and create the NSDictionary/NSArray/NSString/NSNumber objects for you. You don't have to parse anything yourself. You're seeing equal signs and such because you're printing a description of the parsed response object (probably using NSLog).

fumoboy007
  • 5,345
  • 4
  • 32
  • 49
  • ok that does make sense. I am using NSLog to log the NSDictionary or log the JSONKit decoded responseData it does come out different but if I use the same responseData and convert it to a NSString and log it with NSLog I get the proper formatting. Is there another method I can use to log the response to make sure it is encoded properly? Also if I look at the debugger the dictionary that gets created from the JSON shows both "response" and "result" as keys and arrays are the values instead of other dictionaries as the values like it should be – PirateDave Mar 30 '13 at 23:59
  • If you want to see the raw JSON, then just keep doing what you did (converting `NSData` to `NSString` and printing out the string. Regarding your observation of arrays being created instead of dictionaries, I don't see that in your log output; it looks like a dictionary to me. – fumoboy007 Mar 31 '13 at 17:18