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);
1
  • What appears in the console when logging theReply Commented Jun 5, 2013 at 12:56

2 Answers 2

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.

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

6 Comments

added to my answer. Also, sort out you variable names :)
And to avoid magic-numbers you should change option parameter to more human readable: NSJSONReadingMutableContainers
@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.}
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.
Edited the OP to format the JSON. It all looks fine apart from the "hello" on the beginning. Just remove the "hello".
|
-1

Use SBJson library. Its very easy.

5 Comments

I'd always go with NSJSONSerialization as it doesn't rely on third party frameworks and is much faster than SBJSON.
@Fogmeister that is right. But SBJSon saves a lot of codding time for me.
Where I can get example of my situation? I have that library imported.
SBJSON saves time? Why? NSJSONSerialization is always one-liner! developer.apple.com/library/ios/#documentation/Foundation/…
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

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.