3

Using http get request i did get this json string:

hello
[
    {
        "upid":"1",
        "uptitle":"test",
        "uptype":"test",
        "upsize":"1234",
        "upname":"test",
        "upuid":"1",
        "uplocation":"1",
        "uptimestamp":"2013-04-11 09:25:40"
    },
    {
        "upid":"17",
        "uptitle":"test",
        "uptype":"video\/mov",
        "upsize":"2232",
        "upname":"testing",
        "upuid":"1",
        "uplocation":"",
        "uptimestamp":"2013-04-12 11:47:20"
    }
]

How i can get it output in format:

upid:1
uptitle:test
uptype:test
upsize:1234
upname:test
upuid:1
uplocation:1
uptimestamp:2013-04-11 09:25:40

upid:17
uptitle:test
uptype:video\/mov
upsize:2232
upname:testing
upuid:1
uplocation:
uptimestamp:2013-04-12 11:47:20

Here is my code:

NSMutableURLRequest *GETrequest = [[NSMutableURLRequest alloc] init];
    [GETrequest setURL:[NSURL URLWithString:@"http:someip/upload/?id=1&command=alluseruploads&uid=1"]];
    [GETrequest setHTTPMethod:@"GET"];
    [GETrequest setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

// And to send and receive an answer, use a NSURLResponse:
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:GETrequest returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);


// converting string to data
NSData *jsonData = [theReply dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"dic = %@", jsonData);


NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSLog(@"%@", jsonObject);
shim
  • 9,289
  • 12
  • 69
  • 108
Robertas Uldukis
  • 801
  • 1
  • 11
  • 20

2 Answers2

11

You don't have to import any frameworks as this uses a core iOS framework.

If you have an NSString called jsonString then you can do something like this...

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

// you can skip this step by just using the NSData that you get from the http request instead of converting it to a string.

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:&localError];

NSLog(@"%@", jsonObject);

This will log out to the console your object. It will either be an NSArray or an NSDictionary and will contain instances of...

NSDate
NSDictionary
NSArray
NSNumber
NSString

You can then iterate this object to get the data.

OK, using your code...

NSURLResponse *response;
NSData *getData = [NSURLConnection sendSynchronousRequest:getRequest returningResponse:&response error:nil];

NSError* error;

id jsonObject = [NSJSONSerialization JSONObjectWithData:getData options: NSJSONReadingMutableContainers error:&error];

NSLog(@"%@", jsonObject);
NSLog(@"%@", error);

This should create your object. You don't need to convert to string and then back to data.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • added to my answer. Also, sort out you variable names :) – Fogmeister Jun 05 '13 at 12:54
  • And to avoid magic-numbers you should change `option` parameter to more human readable: `NSJSONReadingMutableContainers` – Jakub Jun 05 '13 at 12:56
  • @Fogmeister: thank, fixed variable names :) Can you provide full code with for loop, where i can get my output? Because I'm still getting jsonObject as (null), and error is: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x2fc030 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – Robertas Uldukis Jun 05 '13 at 13:09
  • The problem is that your JSON isn't valid then. If the string you printed in the question beginning with "hello" is what you are receiving then that isn't valid json. – Fogmeister Jun 05 '13 at 13:24
  • Edited the OP to format the JSON. It all looks fine apart from the "hello" on the beginning. Just remove the "hello". – Fogmeister Jun 05 '13 at 13:31
  • @Fogmeister You are genius... Sorted and everything working fine!!!!! It was stupid print command in php code !!! – Robertas Uldukis Jun 05 '13 at 13:43
-1

Use SBJson library. Its very easy.

Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
  • 1
    I'd always go with NSJSONSerialization as it doesn't rely on third party frameworks and is much faster than SBJSON. – Fogmeister Jun 05 '13 at 12:42
  • @Fogmeister that is right. But SBJSon saves a lot of codding time for me. – Durgaprasad Jun 05 '13 at 12:47
  • Where I can get example of my situation? I have that library imported. – Robertas Uldukis Jun 05 '13 at 12:52
  • 2
    SBJSON saves time? Why? NSJSONSerialization is always one-liner! http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – Jakub Jun 05 '13 at 12:53
  • I said it saves time for me. I am using it from many days. It works well for me. In next project I will try NSJSONParser – Durgaprasad Jun 05 '13 at 12:55