0

I am getting some data from WebService (which is a json data)

Exact input sent by Webservice is:

[
    {
        "id": "C-62",
        "title": "testing testing",
        "type": "image",
        "thumb": "",
        "LinkId": "ABC",
        "fileSize":1074,
        "level":0,
    },
    {
        "id": "C-63",
        "title": "testing ab testing",
        "type": "audio",
        "thumb": "",
        "LinkId": "ABCD",
        "fileSize":1074,
        "level":1,
    }

]

As I can see in input data that only fileSize is a integer type Key-Value pair remaining all are in string format.

When I convert that NSData onto NSArray using following code

 NSArray *dataArray = nil;

  dataArray = [NSJSONSerialization JSONObjectWithData:data
                                                     options:0
                                                       error:nil];

I get "dataArray" like this

{
            "id": "C-62",
            "title": "testing testing",
            "type": image,
            "thumb": "",
            "LinkId": "ABC",
            "fileSize":1074,
            "level":0,
        },
        {
            "id": "C-63",
            "title": "testing ab testing",
            "type": audio,
            "thumb": "",
            "LinkId": "ABCD",
            "fileSize":1074,
            "level":1,
        }

The "type" Key-Value pair changes to "int" .All other key-value pairs are in same state.

Please help me in order to convert the input data into the same format as sent by web service. I am using iOS 7.

The dude
  • 7,896
  • 1
  • 25
  • 50
Dinesh Kaushik
  • 2,917
  • 2
  • 23
  • 36
  • 4
    The "type" value appears to me to be an NSString. (When you NSLog an NSDictionary, string values which contain no blanks or "odd" characters are printed without surrounding double-quote symbols.) – Hot Licks May 27 '14 at 14:44
  • In fact, knowing the characteristics of NSLog output I can tell that your bottom listing isn't a copy/paste of the NSLog output, but is simply the JSON text edited. – Hot Licks May 27 '14 at 14:48
  • @HotLicks Thanks for the reply. Your first comment really helped. You can post that as a answer. Real problem was that. In some cases 'fileSize' was arbitrary large . Needed to be considered as long long int. – Dinesh Kaushik May 28 '14 at 10:03

3 Answers3

3

You just got yourself confused. You confuse NSLog output with what is really stored. You are also not saying the truth when you claim that only the "fileSize" field contains numbers, because clearly the "level" field does as well.

NSLog only shows strings with quotes when necessary. A string "10" would be displayed as 10 without the quotes. You can really rely on NSJSONSerializer not doing anything behind your back.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
2

NSJSONSerialization will convert scalar numbers to NSNumbers. That's the only way these can participate in collections (arrays and dictionaries). To convert to int, access the NSNumber and convert it as follows...

NSArray *dataArray = // the de-serialized json
NSDictionary *dictionary = dataArray[0];  // the first dictionary
NSNumber *number = dictionary[@"fileSize"];

// turn an NSNumber representing an integer to a plain int
int fileSizeInt = [number intValue];

But don't try to reinsert that int back into the dictionary.

danh
  • 62,181
  • 10
  • 95
  • 136
  • 1
    The only really touchy thing with NSNumbers in JSON is with really large integers, as they may be encoded as NSDecimalNumber, and you can lose precision if you don't treat them that way. – Hot Licks May 27 '14 at 15:01
  • @danh Thanks for the reply.Comments by 'Hot Licks' solved my problem. – Dinesh Kaushik May 28 '14 at 10:09
1

The one tricky thing about JSON on iOS is figuring out the data type of an arbitrary NSNumber. I chronicled my difficulties in this question.

A large number might be floating-point or integer, and if you attempt to extract one when you really have the other you lose significant digits. And, obviously (in retrospect), if you extract a too-large value into an int you will lose high-order bits and get totally wrong values.

(This isn't quite so difficult if you know in advance that certain values are of certain types. Then it's just a matter of using the proper "verb" to extract the expected type of value.)

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151