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.

3
  • 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.) Commented May 27, 2014 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. Commented May 27, 2014 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. Commented May 28, 2014 at 10:03

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply .comments by 'Hot Licks solved my problem '. Yes I was little confused :)
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.

2 Comments

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.
@danh Thanks for the reply.Comments by 'Hot Licks' solved my problem.
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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.